GNU C++ bug report

Gordon Sadler gbsadler1@lcisp.com
Sun Apr 8 09:04:00 GMT 2001


On Sun, Apr 08, 2001 at 05:27:30PM +0700, ExeBook Support wrote:
> I am using "gcc, g++ - GNU project C and C++ Compiler (gcc-2.95.1)"
> the one shiped with my FreeBSD 4.2
> The following code (see below) produces the incorrect output binary.
> 
> The problem is that the constructor is called once and the destructor is
> called twice.
> 
> Three other compilers I used to compile this example give the correct result
> (i.e. both the desctuctor and constructro are called once). Those compilers
> are Borland C++ builder 5.0, MSVC 6.0 and GNU C++ shipped with Mingw.
> Also the bug appears on the gcc shipped with FreeBSD 3.4 and theone shipped
> with latest OpenBSD (not sure which version).
> 
> People keep telling me not to use such constructions in my programs, but c++
> reference allows it, so I think I should let you know the problem persist.
> If it is possible replyme with comments or let me know when the bug is fixed
> orjust ell me it is already fixed and I just need a newer version of gcc.
> Thank you.
> 
> Yakov Sudeikin
> 
> #include <stdlib.h>
> #include <stdio.h>
> 
> class err {
>  public:
>  ~err()
>  {
>   printf("~err()\n");
>  };
>  err(int i) { printf("err();"); };
> };
> 
> void error(err e)
> {
>  printf("test\n");
> };
> 
> void main()
> {
>  error(2);
> };
> 
I am not an expert, but seems rather simple.

In main you have a call to error(err e),
error(err e) takes an argument of err type,
to create the argument your constructor err() is called,

an implicit copy constructor err(err&) is called....

after error(err e) the destructor ~err() claims the copy,
after main() destructor ~err() claims the first one.

Answer should be to define a copy constructor or use reference
semantics.

HTH

Gordon Sadler



More information about the Gcc mailing list