✅ 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:
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
Same result if finalizer is suppresses or not
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?
Implement a Dispose method
In this article, learn to implement the Dispose method, which releases unmanaged resources used by your code in .NET.
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
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
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
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
Is that dotnet reactive?
Yeah
Oh I am so dumb
I just looked it up, it seems like subscribe gives you an IDisposable
You can dispose in your own dispose
I forgot Subject<T> implements IDisposable. I just learned the surface of it yesterday
Yep
👍
That sounds like the way to go then
Thank ya, rubber ducky moment but was needed
Yw