G
Gutek
Hello all,
I have run into the following problem with virtual destructor. I have
following classes:
class A
{
public:
double* p;
A(unsigned int s=0) : { p = new double; }
virtual ~A() { delete [] p; }
};
class B : public A
{
public:
unsigned int val;
B(unsigned int v=0) : A(v), val(v) { }
B& operator=(const B& b);
~B() { }
};
B& B:perator=(const B& b)
{
this->~B();
val = b.val;
p = new double[val];
copy(b.p, b.p+val,p);
return *this;
}
Now I create a dynamic array of objects B:
B* bp = new B[4];
B b(7);
bp[0] = b;
delete [] bp;
Now at the last line the following run-time error occurs:
Debug Assertion Failed!:
Expression: _CrtIsValidHeapPointer(pUserData)
I have checked that only virtual destructor ~A() is called. The strange
thing is that when destructor ~A() is not virtual or b is assigned to
some other cell, e.g. bp[1] = b;, the problem disappears.
Do you know what is the reson of this?
Regards,
Gutek
I have run into the following problem with virtual destructor. I have
following classes:
class A
{
public:
double* p;
A(unsigned int s=0) : { p = new double
virtual ~A() { delete [] p; }
};
class B : public A
{
public:
unsigned int val;
B(unsigned int v=0) : A(v), val(v) { }
B& operator=(const B& b);
~B() { }
};
B& B:perator=(const B& b)
{
this->~B();
val = b.val;
p = new double[val];
copy(b.p, b.p+val,p);
return *this;
}
Now I create a dynamic array of objects B:
B* bp = new B[4];
B b(7);
bp[0] = b;
delete [] bp;
Now at the last line the following run-time error occurs:
Debug Assertion Failed!:
Expression: _CrtIsValidHeapPointer(pUserData)
I have checked that only virtual destructor ~A() is called. The strange
thing is that when destructor ~A() is not virtual or b is assigned to
some other cell, e.g. bp[1] = b;, the problem disappears.
Do you know what is the reson of this?
Regards,
Gutek