how to mark an object for garbage collection ??

C

cmrchs

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

**********************************************************************
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
 
C

Carl Daniel [VC++ MVP]

Chris said:
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 ?

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
 
W

Willy Denoyette [MVP]

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.
 
C

Carl Daniel [VC++ MVP]

Willy said:
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
 
R

Ronald Laeremans [MSFT]

Carl said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top