[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

c++ managed classes and garbage collectoin

=?Utf-8?B?cm9kY2hhcg==?=

10/30/2003 9:38:00 PM



I am having really tough time finding anywhere on the web
concrete explanation (if any) of how Garbage Colletor
decided for C++ managed objects when object is ready to be
released, and why? Refer to simple example below with
questions inside comments.


__gc public class MyGCClass
{
...
}


void main()
{
simple_unmanaged_function();
}

void simple_unmanaged_function()
{
int x = 0;
MyGCClass* obj = new MyGCClass();
obj->DoSomething();

// Can GC release the MyGCClass instance
// at this point, if it is not being used
// until the end of the function?
// Must GC postpone collecting the instance
// until obj reference to goes out of
// of scope? i.e. for function to return
// What if different optimizations are
// set in "project"->"Property"->
// "c/c++"->"Optimizations"?
//

x = 5;

// Is this (set to NULL) necessary to
// mark obj as not referencing and
// available for GC?
// Or going out of scope is sufficient?
// obj = NULL;
// Seems to work without setting obj to NULL,
// but I just want to know what is good
// practice and why...
}


Thank you,
-Boris
2 Answers

zifengh

11/3/2003 9:41:00 PM

0

As long as your program can access the obj, GC won't collect the memory.
For you case, GC can collect the memory once your function ends and obj is
not accessible from your program.
But when will it be collected is not determined. GC has a good algorithm to
determine when to kick in.

Thanks
Jason

Daniel

11/6/2003 2:47:00 PM

0

if you are using managed C++ then the GC will work as if
you where using any other managed language.

It will be able to collect any object, marked for GC, when
nobody nolonger refers it. So if you look at your code it
COULD be freed after the call if any optimization is
performed by the compiler, but I don't think that any
compiler will perform such optimizations and therefore
your object won't be collected until the end of the
function call. (or it might even be at a much later time
depending on the state of your computer)

Read the articles on MSDN to understand how the GC
operates.

Best regards, Daniel


>-----Original Message-----
>
>
>I am having really tough time finding anywhere on the web
>concrete explanation (if any) of how Garbage Colletor
>decided for C++ managed objects when object is ready to
be
>released, and why? Refer to simple example below with
>questions inside comments.
>
>
>__gc public class MyGCClass
>{
> ...
>}
>
>
>void main()
>{
> simple_unmanaged_function();
>}
>
>void simple_unmanaged_function()
>{
> int x = 0;
> MyGCClass* obj = new MyGCClass();
> obj->DoSomething();
>
> // Can GC release the MyGCClass instance
> // at this point, if it is not being used
> // until the end of the function?
> // Must GC postpone collecting the instance
> // until obj reference to goes out of
> // of scope? i.e. for function to return
> // What if different optimizations are
> // set in "project"->"Property"->
> // "c/c++"->"Optimizations"?
> //
>
> x = 5;
>
> // Is this (set to NULL) necessary to
> // mark obj as not referencing and
> // available for GC?
> // Or going out of scope is sufficient?
> // obj = NULL;
> // Seems to work without setting obj to NULL,
> // but I just want to know what is good
> // practice and why...
>}
>
>
>Thank you,
>-Boris
>.
>