This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin 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]

Problem with virtual inheritance and destruction of arrays


I apologize in advance if the bug has already been reported. Normally when
calling the destructor of an object, the destructors of all the classes in
the hierarchy of inheritance are executed. Imagine you have a virtual
inheritance at some place in the hierarchy, the destruction works well with
a single object, but it doesn't work with an array of objects.

Here is an example I compiled with Cygwin 1.3.2 and Cygwin 1.3.5 using no
particular optimization flag.

#include <iostream.h>

class A {
 protected: int i;
 public: A(int x=5) : i(x) {}
 public: virtual ~A(void) { cout << "A: i=" << i << endl; }
};

class B : public A {
 protected: int j;
 public: B(int x=5) : A(10),j(x) {}
 public: virtual ~B(void) { cout << "B: j=" << j << endl; }
};

class C : public virtual A {
 protected: int k;
 public: C(int x=5) : A(20),k(x) {}
 public: virtual ~C(void) { cout << "C: k=" << k << endl; }
};

int main(void) {
 A * a = new A;
 B * b = new B;
 C * c = new C;

 A * ta = new A[3];
 B * tb = new B[3];
 C * tc = new C[3];

 cout << "destruction object class A" << endl;
 delete a;
 cout << "destruction array class A" << endl;
 delete [] ta;

 cout << "destruction object class B : public A" << endl;
 delete b;
 cout << "destruction array class B : public A" << endl;
 delete [] tb;

 cout << "destruction object class C : public virtual A" << endl;
 delete c;
 cout << "destruction array class C : public virtual A" << endl;
 delete [] tc;

 return (0);
}

Here is the result that illustrates the point. Destruction of the array of
objects of class C didn't execute correctly.

destruction object class A
A: i=5
destruction array class A
A: i=5
A: i=5
A: i=5
destruction object class B : public A
B: j=5
A: i=10
destruction array class B : public A
B: j=5
A: i=10
B: j=5
A: i=10
B: j=5
A: i=10
destruction object class C : public virtual A
C: k=5
A: i=20
destruction array class C : public virtual A
C: k=5
C: k=5
C: k=5

Good luck and "bravo" for the excellent work.


<><><><><><><><><><><><><><><><><><><><>
 Bruno Bachelet (PhD Student)

 LIMOS Laboratory
 ISIMA (Door B112)
 Complexe des Cezeaux
 BP 10125 - 63173 Aubiere Cedex
 France

 Phone: (33).4.73.40.50.44
 Fax: (33).4.73.40.50.01
 Cellular: (33).6.71.94.81.31

 E-mail: bachelet@ifrance.com
 Web: http://bruno.bachelet.net
<><><><><><><><><><><><><><><><><><><><>



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


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