Instantiating Managed structs and classes not as handles

G

Guest

In the following example, where is "n" allocated (e.g. managed heap?) ?

public ref class N
{
....
....
}

main()
{
N^ n_handle; // allocated on managed heap

N n; // Where is this allocated? Not on native stack, right?
}
 
G

Guest

I read that it is automatically allocated on the managed and is a convenience
of C++. Also a destructor and !N() (finalize) methods should be defined for
any cleanup. The destructor can call the finalizer e.g. this->!()N so they
can both use the came code.

The idea is that the destructor will be called for cases of classes
instantiated as local variables (my original posted question) and the
finalizer is called by the GC for normal object instantiations via gcnew.
 
B

Bruno van Dooren

I read that it is automatically allocated on the managed and is a
convenience
of C++. Also a destructor and !N() (finalize) methods should be defined
for
any cleanup. The destructor can call the finalizer e.g. this->!()N so they
can both use the came code.

The idea is that the destructor will be called for cases of classes
instantiated as local variables (my original posted question) and the
finalizer is called by the GC for normal object instantiations via gcnew.

both cases are allocated on the managed heap.
you should have a look at the thread 'Destructor: not guaranteed to be
called?' in this newsgroup, started on 31/01/2006.
It holds an extensive discussion about finalizers, destructors and the
difference between the 2.

--

Kind regards,
Bruno.
(e-mail address removed)
Remove only "_nos_pam"
 
J

Jochen Kalmbach [MVP]

Greg said:
main()
{
N^ n_handle; // allocated on managed heap

N n; // Where is this allocated? Not on native stack, right?
}

Both are allocated on the heap, but the "n" is deterministic calling the
destructor (~) of the class.
It is somehow similar to the "using" statement in C#...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 

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

Top