Naked delegates should have event modifier keyword
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Refactoring
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Ferma
- Stack tecnologico
- csharp, unity
- Ambito
- networking
Direzione di ricerca
Inizia da Runtime/NetworkVariable/NetworkVariable.cs alla riga 23, quindi usa la ricerca del codice del repository collegato per individuare tutte le dichiarazioni pubbliche di callback delegate nel codebase. Controlla ogni delegate non incapsulato e aggiungi il modificatore event dove appropriato; il lavoro è completo quando i callback delegate identificati sono incapsulati in modo coerente come events.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- C#
- Stelle
- 2.3k
- Fork
- 461
- Merge medio
- 3g 16h
- PR unite (30g)
- 20
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Unity-Technologies/com.unity.netcode.gameobjects
-
stat:import type:bug
Difficoltà 4/5 3-5 giorni Idoneità per principianti 68/100
-
stat:reply-needed type:support
Difficoltà 4/5 3-5 giorni Idoneità per principianti 55/100
Unity-Technologies/com.unity.netcode.gameobjects#4095 · 10 commenti ·
-
stat:awaiting-triage stat:Investigating type:bug
Unity-Technologies/com.unity.netcode.gameobjects#3912 · 5 commenti · 1 assegnatario ·
-
Tracking type:feature-2.x
Difficoltà 5/5 Più di una settimana Idoneità per principianti 35/100
Unity-Technologies/com.unity.netcode.gameobjects#3870 · 5 commenti ·
-
Tracking type:feature
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
Unity-Technologies/com.unity.netcode.gameobjects#3830 · 7 commenti ·
Tutte le issue di Unity-Technologies/com.unity.netcode.gameobjects
Issue simili
-
bug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 75/100
sillsdev/languageforge-lexbox#2665 ·
-
bug documentation frontend
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
azurenoops/spin_agent#975 ·
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficoltà 2/5 1-3 ore Idoneità per principianti 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
SubtitleEdit/subtitleedit#15108 · 1 commento ·
-
area/docs-content Bug pulumi/docs
Difficoltà 1/5 1-3 ore Idoneità per principianti 94/100