473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

how to mark an object for garbage collection ??

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...
Nov 17 '05 #1
7 2641
Chris C wrote:
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

Nov 17 '05 #2
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.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:eO*************@TK2MSFTNGP10.phx.gbl...
Chris C wrote:
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

Nov 17 '05 #3
Willy Denoyette [MVP] wrote:
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

Nov 17 '05 #4
Carl Daniel [VC++ MVP] wrote:
Willy Denoyette [MVP] wrote:
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

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...15/417569.aspx

Ronald Laeremans
Visual C++ team
Nov 17 '05 #5
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.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:eO*************@TK2MSFTNGP10.phx.gbl...
Chris C wrote:
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

Nov 17 '05 #6
Willy Denoyette [MVP] wrote:
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

Nov 17 '05 #7
Carl Daniel [VC++ MVP] wrote:
Willy Denoyette [MVP] wrote:
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

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...15/417569.aspx

Ronald Laeremans
Visual C++ team
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ian Renfrew | last post by:
Does Javascript supply a function that destroys an object? If so, is there a dependancy on Javascript version? Thanks in advance, Ian Renfrew
1
by: cmrchs | last post by:
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...
2
by: Yu | last post by:
Hi, Can anyone tell me how I can mark an object for garbage collection in vb.net? Thanks, Yu
9
by: Rob Nicholson | last post by:
Is it possible to trap the creation of a new object and carry out other operations after it's been created? For example, if creating an object of a specific type, then add a reference to a global...
12
by: Mike Eaton | last post by:
Hi all, What do people regard as the best practice with respect to freeing object references when you're done with them? Some people I've worked with in the past suggested that if you create an...
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
13
by: | last post by:
Hi all, Coming from the good old VB6 days we were told to always destroy our objects as follows:- Dim obj as New MyObject ' do some work with obj obj = Nothing I have been doing this in...
20
by: Justin Rich | last post by:
so im trying to be good and not leave anything hanging open but i guess ive seen a variety of ways to kill objects.. is there like a recommended way? basically im looking for best practices for...
11
by: Jay Dee | last post by:
Object garbage collection of events I can not help wondering how the garbage collection system handles events. If I release all references to an object that has a void that an event somewhere...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.