Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / November 2003

Tip: Looking for answers? Try searching our database.

c++ managed classes and garbage collectoin

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
boris - 30 Oct 2003 22:37 GMT
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
Jason He - 03 Nov 2003 21:40 GMT
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 Petersson - 06 Nov 2003 14:47 GMT
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-----
>
[quoted text clipped - 46 lines]
>-Boris
>.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.