Skip to content

Commit

Permalink
Log error on accessing non-initialized lifetime to avoid memory leaks…
Browse files Browse the repository at this point in the history
…. Add `LogErrorIfLifetimeIsNotInitialized` to be able to disable this behaviour
  • Loading branch information
Iliya-usov committed Jan 15, 2025
1 parent fb2cf65 commit ebece17
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions rd-net/Lifetimes/Lifetimes/Lifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,34 @@ public enum LifetimeTerminationTimeoutKind
{

private readonly LifetimeDefinition? myDefinition;
internal LifetimeDefinition Definition => myDefinition ?? LifetimeDefinition.Eternal;

internal LifetimeDefinition Definition
{
get
{
var def = myDefinition;
if (def != null) return def;

if (LogErrorIfLifetimeIsNotInitialized)
{
Log.Root.Error("Lifetime is not initialized. " +
"This may cause a memory leak as default(Lifetime) is treated as Eternal. " +
"Please provide a properly initialized Lifetime or use `Lifetime?` if you need to handle both cases. " +
"Use Lifetime.Eternal explicitly if that behavior is intended.");
}

return LifetimeDefinition.Eternal;
}
}

//ctor
internal Lifetime(LifetimeDefinition definition)
{
myDefinition = definition;
}

[PublicAPI]
public static bool LogErrorIfLifetimeIsNotInitialized = true;


#if !NET35
/// <summary>
Expand Down

0 comments on commit ebece17

Please sign in to comment.