This is the mail archive of the gcc-bugs@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]

bug in g++ 2.7.2.f.1


I believe I've found a bug in g++ version 2.7.2.f.1 compiling on and
for an UltraSparc running Solaris.  (I don't know if local modifications
were made to the source, as I didn't do the installation myself.)

The bug appears to involve an interaction between local class objects with
destructors and local dynamically sized arrays.  Here is an input
program that has incorrect behavior when compiled with g++, either with
no options or with -O2.

#include <stdio.h>

class Test {
public:
  Test() { printf("constructed\n"); }
  ~Test() { printf("destructed\n"); }
};  

void broken(int size){
    Test test;
    if (size) {
      char buffer[size];
      return; /* forgets to call destructor here! */
    }
}

void main() {
  broken(1);
  broken(0);
  broken(1);
  broken(0);
}


The expected output is 4 pairs of "constructed"/"destructed" lines, but
for the two calls to "broken" with non-zero arguments, the "destructed"
line doesn't appear.

The problem is that Test::~Test isn't being called when the function returns
from the nested block.  If the return is removed, then correct behavior ensues.
If the buffer is statically sized or declared in the outer scope, the bug
disappears also.

Thanks for your help.

-- Craig Chambers


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