class base
{
public:
base() : buffer(NULL) {}
private:
unsigned char * buffer;
};
class msgA : public base
{
public:
msgA()
{
//alocate memory for buffer size X
this->buffer = new (std::nothrow) unsigned char[A_MSG_SIZE]; // A_MSG_SIZE = 20
}
};
class msgA2 : public msgA
{
msgA2()
{
//alocate memory for buffer size X2
this->buffer = new (std::nothrow) unsigned char[A2_MSG_SIZE]; // A2_MSG_SIZE = 30
}
};
- is there a memory leak in msgA2 constructor?
- whats the best way to design this without causing any issue?
should I delete buffer then allocate a new one in class msgA2 because msgA constructor were called previously
edit: there is a destructor delete []
I added the following to the constructors
if(this->buffer != NULL)
{
delete [] this->buffer ;
this->pMsgBuffer = NULL;
}