C
C#6mo ago
Jason_Bjorn

✅ Why is the attempted fix still creating a new copy of the dv each time CallDispose is called?

// given, results in "Disposing for first time" being printed 3 times
// static void CallDispose(IDisposable o)
// {
// o.Dispose();
// }

// attempted fix, still prints "Disposing for first time" 3 times
static void CallDispose<T>(T o) where T : IDisposable
{
o.Dispose();
}

DisposableValue dv = new();
Console.WriteLine("Passing value variable:");
CallDispose(dv);
CallDispose(dv);
CallDispose(dv);

public struct DisposableValue : IDisposable
{
private bool _disposedYet;

public void Dispose()
{
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
}
else
{
Console.WriteLine("Was already disposed");
}
}
}
// given, results in "Disposing for first time" being printed 3 times
// static void CallDispose(IDisposable o)
// {
// o.Dispose();
// }

// attempted fix, still prints "Disposing for first time" 3 times
static void CallDispose<T>(T o) where T : IDisposable
{
o.Dispose();
}

DisposableValue dv = new();
Console.WriteLine("Passing value variable:");
CallDispose(dv);
CallDispose(dv);
CallDispose(dv);

public struct DisposableValue : IDisposable
{
private bool _disposedYet;

public void Dispose()
{
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
}
else
{
Console.WriteLine("Was already disposed");
}
}
}
12 Replies
D.Mentia
D.Mentia6mo ago
it's a struct, a value type. Changing its data (_disposedYet) doesn't update the existing one, dv, it makes a new one with the new values which admittedly doesn't sound like it makes much sense when the struct is changing its own values, but I don't think structs typically have any logic in them, assumedly for that reason I'm not sure I explained that accurately but the point is just that the dv you have a reference to is not the DisposableValue that has _disposedYet = true. Basically, use a class imo, or maybe dig into how structs work and maybe if you ref it into CallDispose you can make it work
Jason_Bjorn
Jason_BjornOP6mo ago
I get it I went from boxing to getting a new copy of the value thanks @D.Mentia
D.Mentia
D.Mentia6mo ago
tbh I don't get it :lul: like if it's creating a new DisposableValue when you set _disposedYet... which instance is that method running on, the old one or new one?
Jason_Bjorn
Jason_BjornOP6mo ago
A new one is created on each function call
D.Mentia
D.Mentia6mo ago
like if you did
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
Console.WriteLine(_disposedYet);
}
if (!_disposedYet)
{
Console.WriteLine("Disposing for first time");
_disposedYet = true;
Console.WriteLine(_disposedYet);
}
would it print false?
Jason_Bjorn
Jason_BjornOP6mo ago
In the commented out top one you get a new box with a new copy of the value, in the attempted fix you get a copy of just the value without a box
Passing value variable:
Disposing for first time
True
Was already disposed
Was already disposed
Passing value variable:
Disposing for first time
True
Was already disposed
Was already disposed
D.Mentia
D.Mentia6mo ago
So then... when exactly is the copy created? When it first enters the Dispose() method, it doesn't know that a value is going to be changed... so it must be entering it on the original instance But then upon the value being changed, it creates a new instance, and then continues method execution from within that new instance apparently? IDK, weird
Jason_Bjorn
Jason_BjornOP6mo ago
value types are pass-by-copy
D.Mentia
D.Mentia6mo ago
Oh I see, got it. Even when not changing a value
Jason_Bjorn
Jason_BjornOP6mo ago
Yeah
D.Mentia
D.Mentia6mo ago
you've answered my question, I'll mark this thread as resolved :lul:
Jason_Bjorn
Jason_BjornOP6mo ago
we both learned something
Want results from more Discord servers?
Add your server