destroying value classes

I

Ian Lazarus

Hello,
Since value classes are not managed by the garbage collector and can't have
destructors but can have member data allocated on the C++ heap, how does
this data get destroyed? For example,

__value class vclass
{
public:
vclass()
: p(new int(0))
{
}
int* p; // how does this get destroyed?
};

Thanks
 
D

Derrick Coetzee [MSFT]

Ian Lazarus said:
Since value classes are not managed by the garbage collector and can't have
destructors but can have member data allocated on the C++ heap, how does
this data get destroyed?

In general, you can't. Some options for dealing with this:
* Make it disposable, and add a free() call to the Dispose() method.
* If it's embedded in a reference type, this larger type can reach inside
during its destructor and free data.
* Keep a copy of the pointer in some other location and free it at some
later time.
* Point to a manged type instead of an unmanaged type, for example by
wrapping it with a managed wrapper (such as Int32 in your case), allowing
the GC to free it.

These restrictions were considered reasonable because the purpose of a value
type is to represent a small, simple object with little behaviour, such as a
complex number or a time. If this is a big problem in your application,
perhaps we can come up with a workaround. Can you describe the situation in
more detail, or were you just curious? I hope this helps.
 

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