GC dont call my Dispose-Method although I implemented IDisposable

  • Thread starter Thread starter Jazper
  • Start date Start date
Dude...

that may be true what you are saying. probably there also could be some
errors in my code or some miss-using of things... i don't know. however i my
point of view, it is a bit poor that you have to read a book first to know
how to use such a mechanism. and i doupt that it should be like this.

currently i just have this little problem, that my csharp-application is
allocating memory without releasing it... i think there must be some kind of
crossreference, so that the GC is thinking that this memory is still used.
Dotnet says that the programmer does not have to take care of releasing
memory anymore because of the GC. that was excactly what i did. don't get me
wrong... i like dotnet... i like this... however it did not work because my
mem. raises and raises.

reading books over books about something which i should not care of is some
kind of a waist of time.
i dont think that's the sense of a framework like this. the framework should
reduce your work and developping time. but also there it seems to raise.
:-)
 
thanks for the code. however that did not work.

my finalize-method ~CEmployee() will not be called after all. i think there
must be some kind of
crossreference, so that the GC is thinking that this memory is still used.




Patrick Philippot said:
Mattias said:
Add a call to GC.WaitForPendingFinalizers() if you want to see it
happen earlier.

The call sequence should actually be:

GC.Collect
GC.WaitForPendingFinalizers
GC.Collect

However, I'd like to emphasize that calling GC.Collect is *not* the
normal way of doing things. This method is here only to solve specific
problems. The code above will force finalization but will certainly not
help the application run faster.

--
Patrick Philippot - Microsoft MVP [.Net]
MainSoft Consulting Services
www.mainsoft.xx
(replace .xx with .fr when replying by e-mail)
 
Hi Jazper
my finalize-method ~CEmployee() will not be called after all. i think there
crossreference, so that the GC is thinking that this memory is still used.

Maybe your destructor is being called but it's not caught by the debugger.
Try making it write to some text log file if that's possible, or maybe
display a message box and don't use the debugger. Also there must be some
good reasoning you can't directly implement a finalize method in C#, so you
are more or less trying to fight city hall here. I don't know if you can see
the help index but here are some items you could look up. There are a lot
more. If you need me to cut and paste the entire thing let me know.

Programming for Garbage Collection
Forcing a Garbage Collection
Finalize Methods and Destructors
Cleaning Up Unmanaged Resources

I agree it's not that much fun to read a book and I think you'll do better
with your testing by just using the help page examples. Just keep applying
standard data processing techniques. You know analyze, code, test, analyze,
code, test, analyze, code, test, etc. and yes analyze, code, test yet again
another time...... You'll figure it out if you have the time and desire.
 
GC doesn't call the Dispose method for you because the GC no nothing
about the IDisposable interface. It is your responsibility to call
..Dispose().

GC only know about your finalizer which is ~ClassName().

public MyClass :IDisposable
{
private boolean m_AlreadyDispose = false;

public MyClass() { }
public ~MyClass() { Dispose(); }

public void Dispose()
{
if(m_AlreadyDispose == false)
{
// free up resources.
GC.SupressFinalizer(this);
m_AlreadyDispose = true;
}
}
}

The implementation is roughly like this. To be more exact, check out MSDN.
 
Back
Top