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]
Other format: [Raw text]

[Bug libstdc++/20579] Segmentation fault after exception in list and set destructors


------- Additional Comments From chris at bubblescope dot net  2005-03-21 19:00 -------
This program may make it a bit clearer what is going on :) (the problem is your
code I'm afraid, the compiler is fine)

#include<list>
#include<stdio.h>

struct VAR {
VAR() {printf("VAR created\n");}
~VAR() {printf("VAR destroyed\n");}
};

class TST {
	VAR var;
public:
	TST() 
	{
		printf("begin TST constructor\n");
		delete this;
		printf("just before throw\n");
		throw 1;
	}
	~TST() 
	{ printf("TST Destructor\n"); }
};

int main() {
	TST* tst = NULL;
	try {
		 tst = new TST();
	}
	catch (int i) {
		printf("Caught %d\n",i);
	}
	
}

The output of this program is:

VAR created
begin TST constructor
TST Destructor
VAR destroyed
just before throw
VAR destroyed
Caught 1

What happens is that the TST destructor you call by "delete this" deletes the
member variable. However if an exception is thrown in a constructor then the
compiler will automatically destruct all the member variables, but not call the
destructor. I'm fairly sure that this is correct behaviour.

I'm also generally convinced that you are allowed to call "delete this". You
definatly shouldn't call it in a constructor :)

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20579


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