Naked delegates should have event modifier keyword
Maintainer antworten meist innerhalb von 2 Tagen
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 35/100
- Issue-Typ
- Refactoring
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Veraltet
- Bereich
- networking
Rechercherichtung
Beginne mit Runtime/NetworkVariable/NetworkVariable.cs in Zeile 23 und verwende anschließend die verknüpfte Repository-Codesuche für öffentliche delegate-Callback-Deklarationen im gesamten Codebestand. Prüfe jeden nicht gekapselten delegate und füge, wo angemessen, den event-Modifizierer hinzu; fertig ist die Aufgabe, wenn die identifizierten delegate-Callbacks konsistent als events gekapselt sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- C#
- Sterne
- 2.3k
- Forks
- 464
- Ø Merge
- 2 T. 17 Std.
- Gemergte PRs (30 T.)
- 11
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus Unity-Technologies/com.unity.netcode.gameobjects
-
stat:imported stat:reply-needed type:bug
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 68/100
Unity-Technologies/com.unity.netcode.gameobjects#4159 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 2 Tagen
-
NetworkAnimator: Conditional triggered transitions to Exit node log false errorsEvtl. wieder frei @NoelStephensUnity hat das vor 194 Tagen übernommen, und es ist kein Pull Request offen. Offenstat:awaiting-response stat:awaiting-triage stat:Investigating type:bug
Unity-Technologies/com.unity.netcode.gameobjects#3912 · 7 Kommentare · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Tracking type:feature-2.x
Schwierigkeit 5/5 Über eine Woche Anfängerfreundlichkeit 35/100
Unity-Technologies/com.unity.netcode.gameobjects#3870 · 5 Kommentare ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Tracking type:feature
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 48/100
Unity-Technologies/com.unity.netcode.gameobjects#3830 · 7 Kommentare ·
Maintainer antworten meist innerhalb von 2 Tagen
-
stat:awaiting-response stat:imported type:bug
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 38/100
Unity-Technologies/com.unity.netcode.gameobjects#3802 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 2 Tagen
Alle Issues in Unity-Technologies/com.unity.netcode.gameobjects
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
elsa-workflows/elsa-core#8593 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug good first issue severity:low
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 88/100
-
NEW ISSUE Requestor-TML Maintainers Type: Change/Feature Request
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
tModLoader/tModLoader#5476 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Issue-Enhancement Needs-Triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
PowerShell/PowerShell#28138 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
SubtitleEdit/subtitleedit#15600 ·
Maintainer antworten meist innerhalb von 1 Tag