This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Segfault with delete[] operator & virtually derived classes


Hi there,

I'm writing a memory manager and having some trouble. I have base class A and
derived class B. I am getting a segfault during the delete[] operator. This
simplified test program illustrates the problem:

// Base 
class A {
 public:
 
 A() {};
 virtual ~A() { printf("a"); };

 virtual void func1() {};
 int data1, data2;
};

// Derived
class B : public A {
 public:

 B() {};
 virtual ~B() { printf("b "); };

 int data3, data4;
};

main() {
  A *list = new B[10];      // Allocate 10x derived, store first in A *list
  
  delete[] list;            // This segfaults
  // delete[] ((B *) list); // This doesn't
};

Is it that the array delete operator isn't aware of polymorphism the way the
regular delete operator is? 

I thought about allocating/deleting the list as char * of the appropriate
size, but I want the constructors and vtables to be setup properly.

Stuck.. Don't know how to solve this one.

Help?

Thanks,
-Mercury


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]