<cr*****@gmail.comha scritto nel messaggio
news:d6**********************************@t1g2000p ra.googlegroups.com...
>I have code like this (pseudocode):
MyClass::MyClass()
{
Array1 = new D3DXVECTOR3[MaxVertexCount];
Array2 = new USHORT[MaxIndexCount];
Correct me if I'm wrong, but I think that Array1 and Array2 are defined like
this:
<code>
class MyClass
{
...
D3DXVECTOR3 * Array1;
USHORT * Array2;
...
};
</code>
In that case, the answer to your question is "no": your code is not
exception safe, if an exception is thrown in the constructor, Array1 and
Array2 are leaked.
As Carl already wrote, I think the best approach is to use std::vector
instead of new[] (and delete[]).
e.g.
<code>
#include <vector// Use std::vector
class MyClass
{
....
std::vector< D3DXVECTOR3 Array1;
std::vector< USHORT Array2;
...
};
MyClass::MyClass()
: Array1( MaxVertexCount ), // Construct the std::vector arrays
Array2( MaxIndexCount )
{
... other code
fx = gcnew Effect();
}
</code>
Note that std::vector can change size at run-time (unlike arrays allocated
with new[], which are fixed-size), so you may also want to initialize empty
vectors, and use .push_back() method to add new data to vector.
There is a similar case if you have something like a *raw* pointer to some
class as data member, e.g.:
<code>
class MyClass
{
...
X * pX; // X is some embedded class (raw pointer)
};
MyClass::MyClass()
{
pX = new X(...);
....
}
</code>
The above code is exception unsafe, and if some exception is thrown in the
constructor, the instance of X allocated on the heap is leaked.
You should instead use a smart pointer like shared_ptr in this case:
<code>
class MyClass
{
...
shared_ptr< X spX; // smart pointer to X
};
MyClass::MyClass()
spX( new X(...) ) // Create new X
{
...
}
</code>
In general, in modern C++, you should use container classes like std::vector
instead of raw arrays (new[]/delete[]), and smart pointers (like shared_ptr)
instead of raw pointers. This will make your programming life easier: you
will have less leaks, and more robust and exception-safe code.
Giovanni