Unity-Technologies / Unity-Technologies/com.unity.netcode.gameobjects

Naked delegates should have event modifier keyword

未关闭
#3,137 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

type:feedback
主要语言
C#
星标
2.3k
派生
461
平均合并
3 天 16 小时
30 天内合并 PR
20

描述

https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/blob/dcaeef9ab7271c8e25081b484f9cb0be34294ecd/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariable.cs#L23

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:

  1. Reassignment: External code can reassign the delegate, potentially removing all existing subscribers.

    networkPlayer.OnSpawnedLocal = SomeMethod; // Reassigns the delegate, removing all previous subscribers.
    
  2. 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.
    
  3. 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.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 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

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。