C
C#2y ago
Mango

✅ Implementing IDisposable properly

I have a class I implemented IDisposable in which subscribes to an observable of a type. Example: subscribes to an incoming GenericMessage. I am running into an issue where when an instance of this class should be disposed, it's still alive intercepting data from another class that needs it. Here are my logs for visualization:
[SetResult] Fired for MessageReceiver`1[Messages.GenericMessage]
...
[Dispose] Fired for MessageReceiver`1[Messages.GenericMessage]
...
[SetResult] Fired for MessageReceiver`1[Messages.GenericMessage]
[Fatal] An attempt was made to transition a task to a final state when it had already completed
[SetResult] Fired for MessageReceiver`1[Messages.GenericMessage]
...
[Dispose] Fired for MessageReceiver`1[Messages.GenericMessage]
...
[SetResult] Fired for MessageReceiver`1[Messages.GenericMessage]
[Fatal] An attempt was made to transition a task to a final state when it had already completed
As you see my dispose is called but it's still alive. I am using the dispose design pattern as outlined in the IDisposable docs
15 Replies
Mango
MangoOP2y ago
Same result if finalizer is suppresses or not
Sossenbinder
Sossenbinder2y ago
How did you implement it? Disposing an object is not the same as the object being garbage collected, you just free it from any unmanaged resources or event handler / responsibilities until it is eventually collected by the GC So is there a chance you're not cleaning up an event handler in your dispose, and the object keeps doing work?
Mango
MangoOP2y ago
Implement a Dispose method
In this article, learn to implement the Dispose method, which releases unmanaged resources used by your code in .NET.
Mango
MangoOP2y ago
The dispose pattern section is what I implemented The event isn't class level nor is there an unsubscribe so I need to tell the gc hey destroy this now The event is a subscribe on a Subject<T> for context in a constructor of a class
Sossenbinder
Sossenbinder2y ago
Any chance you got some sample code? Would the object even be cleaned up at this point in time then? Sounds like you have a lingering reference somewhere
Mango
MangoOP2y ago
I cannot share my code but I can write an xy snippet This is what I'd like to accomplish, forcing it to be destroyed when dispose is called
Sossenbinder
Sossenbinder2y ago
Not sure if it's a good idea to try to interfere with the GC. Subject<T> sounds like an Rx thing, wouldn't you also receive a handle or could you inject a pipe takeUntil? I only know rxjs in Angular
Mango
MangoOP2y ago
public GenericMessage()
{
_subject.Subscribe(SetResponse);
}

private void SetResponse(GenericMessage message)
{ _taskCSource.SetResult(messsage);
}
public GenericMessage()
{
_subject.Subscribe(SetResponse);
}

private void SetResponse(GenericMessage message)
{ _taskCSource.SetResult(messsage);
}
Sossenbinder
Sossenbinder2y ago
Is that dotnet reactive?
Mango
MangoOP2y ago
Yeah Oh I am so dumb
Sossenbinder
Sossenbinder2y ago
I just looked it up, it seems like subscribe gives you an IDisposable You can dispose in your own dispose
Mango
MangoOP2y ago
I forgot Subject<T> implements IDisposable. I just learned the surface of it yesterday Yep
Sossenbinder
Sossenbinder2y ago
👍 That sounds like the way to go then
Mango
MangoOP2y ago
Thank ya, rubber ducky moment but was needed
Sossenbinder
Sossenbinder2y ago
Yw
Want results from more Discord servers?
Add your server