Daniel
11/6/2003 2:47:00 PM
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
>.
>