Naked delegates should have event modifier keyword
メンテナーはふだん 3 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- リファクタリング
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 領域
- networking
調査の方向性
Runtime/NetworkVariable/NetworkVariable.cs の 23 行目から始め、次にリンクされた repository のコード検索を使って、コードベース全体にある public delegate callback 宣言を探します。各 naked delegate を確認し、適切な場合は event 修飾子を追加します。特定した delegate callback が一貫して events としてカプセル化されていれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- C#
- スター
- 2.3k
- フォーク
- 465
- 平均マージ
- 3日 12時間
- マージ済み PR(30日)
- 39
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Unity-Technologies/com.unity.netcode.gameobjects のほかの issue
-
TimeSyncMessage is sent reliably, so a late one resets the client clock backwards and stalls its NetworkTransforms対応中かも @reginaldalfret が 8 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 52/100
Unity-Technologies/com.unity.netcode.gameobjects#4249 ·
メンテナーはふだん 3 日以内に返信
-
NetworkTransform: half-float base update dropped as out of order when UseUnreliableDeltas is on, so later deltas decode ~2 m off対応中かも @NoelStephensUnity が 8 日前に担当しました。 オープンport:3.x-completed priority:medium stat:imported type:bug
難易度 4/5 3〜5日 初心者へのやさしさ 57/100
Unity-Technologies/com.unity.netcode.gameobjects#4248 ·
メンテナーはふだん 3 日以内に返信
-
Tracking type:feature-2.x
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
Unity-Technologies/com.unity.netcode.gameobjects#3870 · コメント 5 件 ·
メンテナーはふだん 3 日以内に返信
-
Tracking type:feature
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
Unity-Technologies/com.unity.netcode.gameobjects#3830 · コメント 7 件 ·
メンテナーはふだん 3 日以内に返信
-
priority:medium stat:imported type:feature type:feature-2.x
難易度 4/5 3〜5日 初心者へのやさしさ 42/100
Unity-Technologies/com.unity.netcode.gameobjects#3706 · コメント 5 件 ·
メンテナーはふだん 3 日以内に返信
Unity-Technologies/com.unity.netcode.gameobjects の issue をすべて見る
似ている issue
-
hand-to-AI
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
CactuseSecurity/firewall-orchestrator#5803 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 3 日以内に返信
-
bug python
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
ilia-sokolov/OfficeAgent.NET#89 ·
メンテナーはふだん 13 日以内に返信