diff --git a/rd-net/Lifetimes/Lifetimes/Lifetime.cs b/rd-net/Lifetimes/Lifetimes/Lifetime.cs
index 5c01e6a3e..1fdaa5b5a 100644
--- a/rd-net/Lifetimes/Lifetimes/Lifetime.cs
+++ b/rd-net/Lifetimes/Lifetimes/Lifetime.cs
@@ -117,18 +117,24 @@ internal LifetimeDefinition Definition
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.");
- }
-
+ AssertInitialized();
return LifetimeDefinition.Eternal;
}
}
+ internal void AssertInitialized()
+ {
+ if (!Mode.IsAssertion) return;
+
+ if (LogErrorIfLifetimeIsNotInitialized && myDefinition == null)
+ {
+ 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.");
+ }
+ }
+
//ctor
internal Lifetime(LifetimeDefinition definition)
{
@@ -168,6 +174,16 @@ internal Lifetime(LifetimeDefinition definition)
///
[PublicAPI] public bool IsEternal => Definition.IsEternal;
+ ///
+ /// Whether current lifetime is properly initialized
+ ///
+ [PublicAPI] public bool IsUninitialized => !IsInitialized;
+
+ ///
+ /// Whether current lifetime is not properly initialized and created by default(Lifetime)
+ ///
+ [PublicAPI] public bool IsInitialized => myDefinition != null;
+
///
/// Is of this lifetime equal to
///
diff --git a/rd-net/Lifetimes/Lifetimes/ValueLifetimed.cs b/rd-net/Lifetimes/Lifetimes/ValueLifetimed.cs
index 90a0bc214..e6c70822b 100644
--- a/rd-net/Lifetimes/Lifetimes/ValueLifetimed.cs
+++ b/rd-net/Lifetimes/Lifetimes/ValueLifetimed.cs
@@ -20,13 +20,15 @@ public void Deconstruct(out Lifetime lifetime, out T? value)
public ValueLifetimed(Lifetime lifetime, T value)
{
+ lifetime.AssertInitialized();
+
Lifetime = lifetime;
Value = value;
}
public void ClearValueIfNotAlive()
{
- if (!Lifetime.IsAlive)
+ if (Lifetime is { IsInitialized: true, IsAlive: false })
Value = default(T);
}
}
diff --git a/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs b/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs
index 9455c3383..eae8a71d7 100644
--- a/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs
+++ b/rd-net/Test.Lifetimes/Lifetimes/LifetimeTest.cs
@@ -152,16 +152,25 @@ public void TestEternal()
[Test]
public void TestEquals()
{
- Lifetime eternal = default;
- Assert.AreEqual(Lifetime.Eternal, eternal);
- Assert.AreEqual(Lifetime.Eternal, Lifetime.Eternal);
- Assert.AreEqual(eternal, eternal);
+ var old = Lifetime.LogErrorIfLifetimeIsNotInitialized;
+ Lifetime.LogErrorIfLifetimeIsNotInitialized = false;
+ try
+ {
+ Lifetime eternal = default;
+ Assert.AreEqual(Lifetime.Eternal, eternal);
+ Assert.AreEqual(Lifetime.Eternal, Lifetime.Eternal);
+ Assert.AreEqual(eternal, eternal);
- Assert.True(Lifetime.Eternal == eternal);
+ Assert.True(Lifetime.Eternal == eternal);
- Assert.AreNotEqual(Lifetime.Eternal, Lifetime.Terminated);
- Assert.False(Lifetime.Eternal == Lifetime.Terminated);
- Assert.False(eternal == Lifetime.Terminated);
+ Assert.AreNotEqual(Lifetime.Eternal, Lifetime.Terminated);
+ Assert.False(Lifetime.Eternal == Lifetime.Terminated);
+ Assert.False(eternal == Lifetime.Terminated);
+ }
+ finally
+ {
+ Lifetime.LogErrorIfLifetimeIsNotInitialized = old;
+ }
}
[Test]