A little confused, Dispose().
I have a class BedPlan which is the document of the application. When I call new, if a bed plan is active, I need to clear the current and create a new one.
Do I call Dispose() on this object and create a new object? I am not confident yet on how this garbage collection thing works.
Any suggestions?
Mike B
[335 byte] By [
MikeB] at [2007-11-20 9:16:34]

# 1 Re: A little confused, Dispose().
Unless you have unmanaged resources (such as streams/connections etc.), you just make sure there is no reference to the object instance and mr. Garbage Collector will put it on his list of things to throw away.
BedPlan plan = new BedPlan();
plan = null; // While the instance plan was referencing has not been explicitly destroyed,
// it won't be long until GC comes along and snatches the poor little thing.
You don't need to set the variable to null, you can just set it to another instance right away. The GC just looks for managed objects with no references.
# 2 Re: A little confused, Dispose().
You only need to implement IDisposable and call Dispose if your class has unmanaged members (windows handles, DB connections , etc..) because GC does not detect and free them.
hspc at 2007-11-9 11:35:04 >

# 3 Re: A little confused, Dispose().
Unless you have unmanaged resources (such as streams/connections etc.), you just make sure there is no reference to the object instance and mr. Garbage Collector will put it on his list of things to throw away.
BedPlan plan = new BedPlan();
plan = null; // While the instance plan was referencing has not been explicitly destroyed,
// it won't be long until GC comes along and snatches the poor little thing.
You don't need to set the variable to null, you can just set it to another instance right away. The GC just looks for managed objects with no references.
You only need to implement IDisposable and call Dispose if your class has unmanaged members (windows handles, DB connections , etc..) because GC does not detect and free them.
Nice, thanks. I have to tell ya, I have been programming in c++ for a few years now, and I am really liking this c#.
Mike B
MikeB at 2007-11-9 11:36:04 >

# 4 Re: A little confused, Dispose().
imho it's bad just to leave everything up to the GC
if one does a really mem using application
Gc is simply not fast enough for semi temp "leaks" to happen
gc is a safetynet not a sleeping pillow :P
# 5 Re: A little confused, Dispose().
imho it's bad just to leave everything up to the GC
if one does a really mem using application
Gc is simply not fast enough for semi temp "leaks" to happen
gc is a safetynet not a sleeping pillow :P
Buh? Nothing* can leak from the GC. If you're instantiating unmanaged resources there are is only 1 thing you *have* to do to guarantee you won't permanently lose track of your unmanaged memory, and that's override the destructor and release your unmanaged resources in it.
The GC is a sleeping pillow and a very comfortable one at that.
*i believe there are things which can 'leak' in the classical sense, but those are few and far between.
# 6 Re: A little confused, Dispose().
imho it's bad just to leave everything up to the GC
if one does a really mem using application
Gc is simply not fast enough for semi temp "leaks" to happen
gc is a safetynet not a sleeping pillow :P
In .net, what is the alternative? You cannot free the memory yourself.