help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

STL Container leaks/exception problem


From: Kenneth Karoliussen
Subject: STL Container leaks/exception problem
Date: Wed, 07 Jun 2006 13:51:27 +0200
User-agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.13) Gecko/20060508

Hi,

I'm currently working on FreeBSD with and using GNU g++ v3.4.4.

The following code leaks and sometimes crashes with a bus error.

I've identified the problem and simplified the scenario where problem occurs in my project.

Consider the following code:

#include <string>
#include <iostream>

using namespace std;

class A
{
public:
        A() { cerr <<"A CTOR. called." <<endl; }
        
        A(const A& Other) { cerr <<"A COPY ctor. called." <<endl; }
        
        A& operator=(const A& rhs)
        {
                cerr <<"A operator=" <<endl;
                return *this;
        }       
        string say() const { return "Hello world!"; }

        virtual ~A(){ cout <<"A dtor called" <<endl; }
};

A getA() {
        throw "Test exception, abort getA()!";
        return A();
}

void sayA(A a)
{
        a.say();
}

int main(int argc, char* argv[])
{               
        while (true)
        {               
                try {
                        sayA(getA());
                        
                } catch (const char* const c) {
                        if (c) cerr <<"Exception: " <<c <<endl;
                }
        };
        
        return 0;
}


The problem (leak) occures after the throw statement in getA() for some reason. The ctor. of A isn't even called, as expected.

I cannot replicate the leak (seems stable) with the following conditions:

1. const A& refA = getA(); No problem occurs. Goes to exception cache without any leaks.
2. Insert a new clean copy of A() into getA().
3. Remove the throw statement in getA().
4. sayA with reference argument of A object (no temporary copy).
5. MS VC8 and GNU g++ v3.3.x does not have any leaks.

I'm a bit baffled why the leak occurs. Could be that I overlooked something essensial or the problem is more complex (related to the compiler).

It seems that there may be some kind of problem with temporary and the throw statement, but it is just a raw assumption.

Any insight and ideas are greatly appreciated.

Thanks!


">
reply via email to

[Prev in Thread] Current Thread [Next in Thread]