[Bug c++/15424] New: c++: destructor not called for objects in array

wouter dot vermaelen at pi dot be gcc-bugzilla@gcc.gnu.org
Fri May 14 12:38:00 GMT 2004


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



More information about the Gcc-bugs mailing list