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 / Languages / Managed C++ / June 2005

Tip: Looking for answers? Try searching our database.

how to mark an object for garbage collection ??

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cmrchs@yahoo.com - 27 Jun 2005 17:49 GMT
Hi,

how do you mark an object for garbage collection in C++.NET 2005 ?

 MyDbClass^ obj = gcnew MyDbClass();

 // Release the object
 obj = 0;  --> COMPILER ERROR
 obj = nullptr;  // nope : this does not mark it for garbage
                 //        collection  (see help)

so ... how ?

thanks
Chris
Carl Daniel [VC++ MVP] - 27 Jun 2005 19:13 GMT
Chris C wrote:
> Hi,
>
[quoted text clipped - 8 lines]
>
> so ... how ?

You do not "mark objects for garbage collection".  You simply stop
referencing them (e.g. by setting your reference to nullptr) and the GC
_MAY_ come along and collect the object at some point in the future.

If you need deterministic destruction, allocate the object "on the stack":

MyDbClass obj();

The compiler will translate this to something like:

MyDbClass^ obj=null;
try {
   obj = gcnew MyDbClass();
    // do stuff with object
}
finally {
   if (null != obj)
       obj.Dispose();
}

-cd
Willy Denoyette [MVP] - 28 Jun 2005 17:52 GMT
Allocating an object "on the stack" will not trigger a GC either when it
goes out-of scope. Provided it has a destructor, Dispose() is automatically
called when it leaves the scope, but that's it. The object will be collected
non-deterministically when the GC comes along.

Willy.

> Chris C wrote:
>> Hi,
[quoted text clipped - 31 lines]
>
> -cd
Carl Daniel [VC++ MVP] - 29 Jun 2005 00:19 GMT
> Allocating an object "on the stack" will not trigger a GC either when
> it goes out-of scope. Provided it has a destructor, Dispose() is
> automatically called when it leaves the scope, but that's it. The
> object will be collected non-deterministically when the GC comes
> along.

I believe that's what I said.  Bottom line for the OP: there is no such
thing as marking an object for collection or forcing an object to be
collected.  You can force an object to be disposed, which is the technique
of preference for situations where you need deterministic cleanup of
non-memory resources (like DB connections).

-cd
Ronald Laeremans [MSFT] - 29 Jun 2005 06:59 GMT
>>Allocating an object "on the stack" will not trigger a GC either when
>>it goes out-of scope. Provided it has a destructor, Dispose() is
[quoted text clipped - 9 lines]
>
> -cd

Also setting the reference to nullptr for a local does nothing useful
whatsoever. The JIT compiler marks GC lifetime regions, so if after a
certain point in the IL you no longer reference this object, the memory
it uses can be collected from that time on regardless of whether the
local reference still points to it.

Look here for the details:
http://blogs.msdn.com/yunjin/archive/2005/05/15/417569.aspx

Ronald Laeremans
Visual C++ team

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.