Naked delegates should have event modifier keyword
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Refactor
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp, unity
- Domain
- networking
Research direction
Start with Runtime/NetworkVariable/NetworkVariable.cs at line 23, then use the linked repository code search for public delegate callback declarations across the codebase. Check each naked delegate and add the event modifier where appropriate; done means the identified delegate callbacks are consistently encapsulated as events.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- C#
- Stars
- 2.3k
- Forks
- 461
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 20
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Unity-Technologies/com.unity.netcode.gameobjects
-
stat:import type:bug
Difficulty 4/5 3-5 days Newbie friendliness 68/100
-
stat:reply-needed type:support
Difficulty 4/5 3-5 days Newbie friendliness 55/100
Unity-Technologies/com.unity.netcode.gameobjects#4095 · 10 comments ·
-
stat:awaiting-triage stat:Investigating type:bug
Unity-Technologies/com.unity.netcode.gameobjects#3912 · 5 comments · 1 assignee ·
-
Tracking type:feature-2.x
Difficulty 5/5 Over a week Newbie friendliness 35/100
Unity-Technologies/com.unity.netcode.gameobjects#3870 · 5 comments ·
-
Tracking type:feature
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Unity-Technologies/com.unity.netcode.gameobjects#3830 · 7 comments ·
All issues in Unity-Technologies/com.unity.netcode.gameobjects
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·