Unity-Technologies / Unity-Technologies/com.unity.netcode.gameobjects
Naked delegates should have event modifier keyword
還沒有人認領這個 Issue。
- 主要語言
- C#
- 星號
- 2.3k
- 分支
- 461
- 平均合併
- 3 天 16 小時
- 30 天內合併 PR
- 20
描述
Delegate events like the one shown above should have event keyword modifiers.
public event OnValueChangedDelegate OnValueChanged;
A "naked" delegate refers to a delegate that is declared without the event keyword. This means the delegate is exposed directly, allowing external code to not only subscribe and unsubscribe to it but also to reassign or invoke it directly. This can lead to unintended side effects and potential bugs.
For example, in the provided code snippet, the delegates are correctly declared as events:
public event Action OnSpawnedLocal;
public event Action OnSpawnedAll;
public event Action<NetworkPlayer> OnDisconnected;
If these were declared as naked delegates, they would look like this:
public Action OnSpawnedLocal;
public Action OnSpawnedAll;
public Action<NetworkPlayer> OnDisconnected;
Issues with naked delegates:
-
Reassignment: External code can reassign the delegate, potentially removing all existing subscribers.
networkPlayer.OnSpawnedLocal = SomeMethod; // Reassigns the delegate, removing all previous subscribers. -
Invocation: It breaks encapsulation, making it harder to control how and when the delegate is invoked.
networkPlayer.OnSpawnedLocal?.Invoke(); // Invokes the delegate from outside the class. -
Dangling Pointers: Events in C# are multicast delegates which can have multiple and anonymous methods subscribe to the same event. Using the event keyword will help properly manage those subscriptions and unsubscriptions. This way if someone forgets to unsubscribe, the dangling pointer will get properly cleaned up by garbage collector.
- If a delegate holds a reference to an object that should be garbage collected, it can prevent the object from being collected, leading to memory leaks.
- If the target method or object of a delegate has been collected, invoking the delegate can result in a NullReferenceException or other runtime errors.
TR;DL:
I've seen this throughout the whole codebase and highly encourage the use of the event keyword for all usages of delegate callbacks.
Using the event keyword ensures that the delegate can only be invoked from within the class that declares it, maintaining proper encapsulation and control over the event's lifecycle and avoiding memory pressure from forgetting to unassign the delegates after use.
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
從 Runtime/NetworkVariable/NetworkVariable.cs 的第 23 行開始,接著使用連結的 repository 程式碼搜尋功能,在整個程式碼庫中尋找 public delegate callback 宣告。檢查每個未封裝的 delegate,並在適當的地方加入 event 修飾子;當已識別的 delegate callback 都一致地封裝為 events 時,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- csharp, unity
- 領域
- networking
- Issue 類型
- 重構
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100