Re: exception question,
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Wed, 6 Aug 2008 21:49:51 -0700
cronusf@xxxxxxxxx wrote:
I have code like this (pseudocode):
MyClass::MyClass()
{
Array1 = new D3DXVECTOR3[MaxVertexCount];
Array2 = new USHORT[MaxIndexCount];
fx = gcnew Effect();
}
Suppose the constructor for Effect throws an exception. During the
stack unwinding, will Array1 and Array2 be deleted?
I put the delete[] code in the destructor and finalizer, but I noticed
(through trace statements) the destructor and finalizer is never
called. Do I need a try/finally here to make sure the memory is
deleted?
What's the type of Array1? Array2?
Unless it's some kind of smart pointer that takes care of deleting the
allocated memory, then you need to take responsibility for doing it.
The best option is to use a more intelligent data structure that takes care
of it for you, such as std::vector<D#DXVECTOR3> or std::vector<USHORT>,
rather than a bare pointer. When those objects go out of scope, their
destructors will be called automatically to clean up the memory allocation.
If you're set on handling it yourself instead of using a library component
to do it for you then yes, you need a try/catch around the code to handle
cleanup in the case of an exception thrown from the Effect constructor.
-cd
.
- References:
- exception question,
- From: cronusf
- exception question,
- Prev by Date: Re: exception question,
- Next by Date: Re: A question about WaitForMultipleObjects
- Previous by thread: Re: exception question,
- Next by thread: Re: exception question,
- Index(es):
Relevant Pages
|