MC ++ reference types and their scope

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I'm back from some old fashioned project and something has changed in
comparison to my first hours of programming in Visual C++. I'm up to build a
tool and started by means of the Express Edition 2005 of VC++ aka VCM++.

I decided to use the advantages of Managed Code but I'm wondering about the
designer generated code.

It's a Windows Form App. I use Managed Strings and other built in types.

My question is if I instantiate an object of type Int32 or Double or String
have I order to delete such objects explicitly or is GC doing it.

As I can see the code designer doesn't add code to order to destroy such
objects explicitly but for one it is doing it.

System::ComponentModel::Container ^components;

and in the destructor

if (components) {
delete components;
}

As I read all members will be implicitly destroyed when they come out of
scope.
Does this also include the reference members?
I read some articles from the msdn mag and know how the finalize mechanism
works but I'm just unsure since as I started to program I used the old new,
delete mechanism which is obsolete now. Please let me know what can I do to
avoid misdesigns. May be it's to basic for the good old programer on this
thread but please help. Thanks in advance.
 
Bastian,
I'm back from some old fashioned project and something has changed in
comparison to my first hours of programming in Visual C++.

Yes, indeed, there were some minor changes ;-)

My question is if I instantiate an object of type Int32 or Double or
String have I order to delete such objects explicitly or is GC doing
it.

Basically, everything is automatically destroyed, when not referenced
any more or going out of scope. The only thing you have to deal with is
unmanaged code, e.g. a System.Drawing.Image type. There you must
explicitly delete the image, otherwise only the pointer to this
unmanaged resource will be freed and the image data will still be kept
in memory.


hth
Markus
 
Oh yes, thats what I realized as I thought about this thematik again. But
thank you for your reply. With your post I'm much more sure now.
 
Back
Top