Question about garbage collection

  • Thread starter Thread starter Howard Weiss
  • Start date Start date
H

Howard Weiss

I am writing a Managed C++ application.

In my application, I create a modeless sub-form using New. I retain a
pointer to this sub-form in my application.

If the user closes this form, my pointer is still valid. If I interrograte
the instance of the sub-form, the IsDisposed attribute is set.

I want to recreate the sub-form. To do so, I call new again and set my
pointer to the new instance of the sub-form.

What happens to the memory allocated to the old instance of the sub-form? I
am hoping that the framework is keeping reference counts and recognizes when
the old instance is no longer needed and eventually garbage collects the
data.

Can someone verify that this indeed occurs?

Howard Weiss
 
Rather than just answering your question, I'm going to give you a couple
links that talk about the Garbage Collection :-)

http://msdn.microsoft.com/msdnmag/issues/1100/gci/
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetGCbasics.asp


IsDisposed just means that the object has been disposed however - the object
and its data can still be in memory until actual garbage collection occurs
and the object is compacted out of the managed heap.

Ahh - well - i'll give an answer a shot -

There are so many nuances to the GC process - so I'm not going to give a
straight answer - but - the memory should eventually be collected - but
when/if also depends on how long the form has been around - if it has made
it into gen1 or gen2 - what you have done in your code with your form
reference - what kind of objects you have created on your form - if there
are any finalizers to be called - and other variations.

Part of writing managed applications is that as a whole - you don't really
have to worry about what the CLR is doing with your memory - although - I
highly recommend reading those articles I posted up there - because you can
still really muck your application up but not using your memory wisely.

And I also have to give the disclaimer - that I haven't written managed C++
applications - the C++ stuff I wirte is unmanaged - and the managed stuff I
write is C# - so there is a possiblity that Managed C++ has a different
answer (although by deffinition of 'managed' - it shouldn't).

Anway, I hope the links are useful - and my vague answer made you want to
read the links. :)
 
Howard said:
I am writing a Managed C++ application.

In my application, I create a modeless sub-form using New. I retain a
pointer to this sub-form in my application.

If the user closes this form, my pointer is still valid. If I interrograte
the instance of the sub-form, the IsDisposed attribute is set.

I want to recreate the sub-form. To do so, I call new again and set my
pointer to the new instance of the sub-form.

What happens to the memory allocated to the old instance of the sub-form? I
am hoping that the framework is keeping reference counts and recognizes when
the old instance is no longer needed and eventually garbage collects the
data.


Yes, it gets garbage collected as far as the form or any of its contents
is not pointed by any other pointer.

However it is not garbage collected immediately but when the CLR decides.






Best regards,

Ioannis Vranos
 
Back
Top