C
C#13mo 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
Mango13mo ago
Same result if finalizer is suppresses or not
Sossenbinder
Sossenbinder13mo 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
Mango13mo 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
Mango13mo 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
Sossenbinder13mo 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
Mango13mo 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
Sossenbinder13mo 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
Mango13mo 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
Sossenbinder13mo ago
Is that dotnet reactive?
Mango
Mango13mo ago
Yeah Oh I am so dumb
Sossenbinder
Sossenbinder13mo ago
I just looked it up, it seems like subscribe gives you an IDisposable You can dispose in your own dispose
Mango
Mango13mo ago
I forgot Subject<T> implements IDisposable. I just learned the surface of it yesterday Yep
Sossenbinder
Sossenbinder13mo ago
👍 That sounds like the way to go then
Mango
Mango13mo ago
Thank ya, rubber ducky moment but was needed
Sossenbinder
Sossenbinder13mo ago
Yw
Want results from more Discord servers?
Add your server
More Posts
❔ Help with Assemblies and the TypeRef tableI am trying to load a .NET DLL as an assembly and read the TypeRef table. End goal is to get all the❔ Python Decorators in C# ??? How do i intercept method calls with attributes?Is is possible to intercept/prevent a method call in c# with attributes? i was thinking about someth❔ AspNetCore web MVC app - AuthenticationI would like to implement some authentication to my web app. I would like to make it "proper" and no❔ .net 7 docker container error: qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2' HELPI am getting this error (qemu-x86_64: Could not open '/lib64/ld-linux-x86-64.so.2': No such file or ✅ Cant open .csporjSo i made a WFA and i want to edit the .csproj file but i cant open it✅ Restore a PackageReference and use contained MSBBuild scripts inside of .targets fileHey folks, I have a CICD script that executes a custom target `Foo.targets` using `dotnet msbuild F❔ Cross-platform 3D rendering library?Hello, I am working on an application/tool to generate 3D models. I was wondering if there was a goo✅ How to make a custom converter in .net 6.0 using wpf?Hello, sorry I couldn't find anything useful using google so i'm asking here, sorry if it's obvious.❔ How does the main method work?I've only ever used c# in unity, so i've never worked outside the context of making games, so using ❔ ✅ How to implement pool tracking in concurrent list ?I am trying to add tracking to my logging service. (Web API project) Once track count is matched, I