Instantiating Managed structs and classes not as handles

  • Thread starter Thread starter Guest
  • Start date Start date
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?
}
 
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.
 
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"
 
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/
 
Back
Top