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... 7 2668
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
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-deterministical ly when the GC comes along.
Willy.
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:eO******** *****@TK2MSFTNG P10.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
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-deterministical ly 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
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 automatical ly called when it leaves the scope, but that's it. The object will be collected non-deterministical ly 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
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-deterministical ly when the GC comes along.
Willy.
"Carl Daniel [VC++ MVP]" <cp************ *************** **@mvps.org.nos pam>
wrote in message news:eO******** *****@TK2MSFTNG P10.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
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-deterministical ly 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
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 automatical ly called when it leaves the scope, but that's it. The object will be collected non-deterministical ly 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 does not mark it for garbage
// collection (see help)
|
by: Yu |
last post by:
Hi,
Can anyone tell me how I can mark an object for garbage
collection in vb.net?
Thanks,
Yu
|
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 collection.
Thanks, Rob.
|
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 object with "New" then you should free/set it to nothing when you're finished with it, regardless of whether it's about to go out of scope or not.
...
| |
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.
Recently we (my fellow team members & I) observed an
InvalidOperationException - 'collection has been modified', however it
wasn't immediately...
|
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 .Net also for quite a while now. The other day one
|
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 handling object destruction.
Thanks
Justin
|
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 is using I then have no access to the class holding the void
so it should be garbage collected.
But if an event holds a reference to a void of...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |