Automatic Destructor

Nicolas Goniak goniakn7@cti.ecp.fr
Wed Nov 19 07:39:00 GMT 1997


Hope this bug is new to you.
Snapshot used : 1997-11-14

In the following code, the automatic destructor of the Bozo instance does
not do its job very well. The program fills very quickly all available
memory.


gcc -g bozo.cc
gcc -g main.cc
gcc -g main.o bozo.o -lstdc++
./a.out


//--------------------------------------------------
//
//   main.cc
//

#include "bozo.hh"

int main (int argc, char* argv[])
{
  for (unsigned long i= 0 ; i < 0xffffffff ; i ++)
    {
      Bozo bozo;
      bozo.log();
    }
}

//---------------------------------------------------
//
// bozo.hh
//
//

#include <string>
#include <list>

struct Bozo {
  void log(void);

  int junk;
  list<string> truc_list;
};

//---------------------------------------------------
//
// bozo.cc
//

#include "bozo.hh"


void Bozo::log(void)
{
  truc_list.push_back("agagagagagagagagagagagagag");
}



By the way, the problem disappears if we provide a constructor AND a
destructor to Bozo class : 




//---------------------------------------------------
//
// bozo.hh
//
//

#include <string>
#include <list>

struct Bozo {
  Bozo();
  ~Bozo();
  void log(void);
  
  int junk; 
  list<string> truc_list;
};

//---------------------------------------------------
//
// bozo.cc
//
  
#include "bozo.hh"

Bozo::Bozo()
{
  junk = 0;
} 

Bozo::~Bozo()
{
  junk = 0;
}
  
void Bozo::log(void)
{
  truc_list.push_back("agagagagagagagagagagagagag");
}



What is more, the problem totally disappears if all declarations are made
in main.cc

gcc -g main.cc -lstdc++
./a.out



//--------------------------------------------------
//
//   main.cc
//

#include <string>
#include <list>

//
// Bozo struct
//

struct Bozo {
  void log(void);

  int junk;
  list<string> truc_list;
};

void Bozo::log(void)
{
  truc_list.push_back("agagagagagagagagagagagagag");
}

//
// main program
//

int main (int argc, char* argv[])
{
  for (unsigned long i= 0 ; i < 0xffffffff ; i ++)
    {
      Bozo bozo;
      bozo.log();
    }
}

Thanks for listening.


Nicolas Goniak, goniakn7@cti.ecp.fr
Eleve-Ingenieur a l'Ecole Centrale Paris.
Student in Engineering at Ecole Centrale Paris.




More information about the Gcc-bugs mailing list