This is the mail archive of the
egcs-bugs@egcs.cygnus.com
mailing list for the EGCS project.
Throw & catch bug?
- To: egcs-bugs@egcs.cygnus.com
- Subject: Throw & catch bug?
- From: jorgefm@cirsa.com
- Date: Wed, 7 Jul 1999 10:16:39 +0200
Hi
I have the next program:
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
class TException
{
public:
TException(const char *why );
virtual ~TException();
virtual char *GetMsgError() { return m_why; }
protected:
char *m_why;
};
TException::TException(const char *why)
{
printf("Begin Constructor\n");
m_why = (char *)malloc( strlen( why ) + 1 );
if( m_why != NULL ) {
strcpy( m_why, why );
printf( " Error Message: \"%s\"\n", GetMsgError() );
}
printf("End Constructor\n");
}
TException::~TException()
{
printf("Begin Destructor\n");
if( m_why != NULL ) {
free( m_why );
m_why = NULL;
}
printf("End Destructor\n");
}
int main(void)
{
try {
printf( "Before Throw\n" );
throw TException( "Generate exception" );
printf( "After Throw\n" );
} catch(TException& e) {
printf("Catch \n");
printf( " Error: \"%s\"\n", e.GetMsgError() );
}
return 0;
}
And the output is:
Before Throw
Begin Constructor
Error Message: "Generate exception"
End Constructor
Begin Destructor
End Destructor
Catch
Error: "0?$¡@¡"
Begin Destructor
End Destructor
How can the destructor call twice, one after throw (incorrect) and the
second and ok at the end of the catch.
I have gcc version egcs-2.91.66 19990314/Linux (egcs-1.1.2 release). That's
a known bug?. Anybody knows a
solution. I have try with 'new' and 'delete' and its ok.
try {
throw new TException( .. );
} catch( TException *e) {
e->Get...
delete e;
}
jorgefm@lgcirsa.com
Thanks for your time.