C
C#2y ago
Sal

❔ Destroy objects in CSharp, IDisposable

Hi, I am trying to get an object destroyed but it doesn't seem to be working. I implemented IDisposable interface and started by just adding the Dispose() method, without any "destruction" I tried as they show on the documentation (simplified as far as I understand).
bool disposed
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (disposed)
return;
}
bool disposed
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (disposed)
return;
}
I also did use the
using (var obj = new MyObject())
{
//some code
}
//check for obj
using (var obj = new MyObject())
{
//some code
}
//check for obj
and when I checked for the object it was there still any help with this?
6 Replies
Sal
SalOP2y ago
btw I understand the garbage collector takes care of destroying instances but I would really want to control this one, and make sure its no longer there
Angius
Angius2y ago
Objects being disposed doesn't mean they're being collected You can use GC.Collect() to force the garbage collector to run, but it's usually a bad idea You could try asking in #advanced maybe. Generally speaking, though, just let the garbage collector do its job
Sal
SalOP2y ago
ok thanks I will try
mtreit
mtreit2y ago
You really can't deterministically free managed memory in c#. It's not really even worth trying. If you really need that you can allocate and free blocks of native memory, but of course then you generally have to work with primitive types.
Sal
SalOP2y ago
that sounds really complicated x) i just wanted to see the destructor in action, but the way i am doing i dont reach it
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server