This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/15424] New: c++: destructor not called for objects in array
- From: "wouter dot vermaelen at pi dot be" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 13 May 2004 18:21:34 -0000
- Subject: [Bug c++/15424] New: c++: destructor not called for objects in array
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
Sometimes the object in an array are not destroyed when the array goes out of
scope. It only seems to happen with objects of templatized classes and when the
size of the array is not explictly specified.
1 #include <iostream>
2 template <typename T>
3 struct A
4 {
5 A(T* t_) : t(t_) { std::cout << "A::A() " << *t << std::endl; }
6 ~A() { std::cout << "A::~A() " << *t << std::endl;
7 delete t; }
8 T* t;
9 };
10 struct B
11 {
12 B() { std::cout << "B::B()" << std::endl; }
13 ~B() { std::cout << "B::~B()" << std::endl; }
14 };
15 int main()
16 {
17 A<int> array1[] = { A<int>(new int(1)) }; // wrong
18 A<int> array2[1] = { A<int>(new int(2)) }; // ok
19 B array3[] = { B() }; // ok
20 }
Output when compiled with gcc. I tried version 2.95, 3.2.3 and 3.3.3. icc gives
the expected result.
A::A() 1 <-- constructed but not destructed
A::A() 2
B::B()
B::~B()
A::~A() 2
The bug disappears When line 17 and 18 are swapped in this test.
--
Summary: c++: destructor not called for objects in array
Product: gcc
Version: 3.3.3
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: wouter dot vermaelen at pi dot be
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15424