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

Naked delegates should have event modifier keyword

Ouverte
#3,137 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

type:feedback
Langage dominant
C#
Étoiles
2.3k
Forks
461
Merge moyen
3 j 16 h
PR mergées (30 j)
20

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par Runtime/NetworkVariable/NetworkVariable.cs à la ligne 23, puis utilisez la recherche de code du repository lié pour trouver les déclarations publiques de callback delegate dans l’ensemble du code. Vérifiez chaque delegate non encapsulé et ajoutez le modificateur event lorsque cela est approprié ; le travail est terminé lorsque les callbacks delegate identifiés sont systématiquement encapsulés en tant qu’events.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
csharp, unity
Domaine
networking
Type d'issue
Refactorisation
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.