c++/9075: Destructor is called more times than required..

sunil.k.davasam@intel.com sunil.k.davasam@intel.com
Fri Dec 27 19:56:00 GMT 2002


>Number:         9075
>Category:       c++
>Synopsis:       Destructor is called more times than required..
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Dec 27 19:56:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     sunil.k.davasam@intel.com
>Release:        g++3.2
>Organization:
>Environment:
$ g++ -v
Reading specs from /local/skdavasa/gcc321/lib/gcc-lib/i386-redhat-linux/3.2.1/specs
Configured with: gcc-3.2.1/configure --prefix=/local/skdavasa/gcc321 --enable-shared --enable-threads=posix --disable-checking --host=i386-redhat-linux --with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 3.2.1
$ cat /etc/issue
Red Hat Linux release 8.0 (Psyche)
Kernel \r on an \m
>Description:
I have a small testcase.

[skdavasa@yeti testing]$ cat t1.cpp 
#include <stdio.h>

class C {
  public:
    C() { 
            printf("C()\n");
    }
    ~C() { 
            printf("~C()\n"); 
    }
};


void func(C c)
{
    printf("func()\n");
}

int main(void)
{
    C c;

    func(c);
    printf("main()\n");
    return 0;
}

  When I compiled and ran the above the test case, I found that destructor is called twice, where as constructor is called only once. I think it is wrong.
>How-To-Repeat:
$ g++ t1.cpp 
$ a.out
C()
func()
~C()
main()
~C()
---

Where as expected output:
------
C()
func()
main()
~C()
--------
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="t1.cpp"
Content-Disposition: inline; filename="t1.cpp"

#include <stdio.h>

class C {
  public:
    C() { 
	    printf("C()\n");
    }
    ~C() { 
	    printf("~C()\n"); 
    }
};


void func(C c)
{
    printf("func()\n");
}

int main(void)
{
    C c;

    func(c);
    printf("main()\n");
    return 0;
}



More information about the Gcc-prs mailing list