hi all.
i really fail to see what i'm doing wrong in the following code:
struct Exception : std::exception {
Exception (const char *format, ...) __attribute__((__format__ (__printf__, 2,
3)));
[...]
};
struct Obj {
Obj () { throw Exception ("%s: error", typeid (this).name()); }
};
try { Obj a; } catch (std::exception &e) {
printf ("exception: what(): %s\n", e.what());
}
it does however produce garbage (with 3.3 and 3.4) instead of Obj's typename:
exception: what(): 44`l$@: error
i have attached a full test case with 2 throw statements that
will both produce a garbage typename. i'm not sure this isn't
a compiler bug, so any help is apprechiated.
---
ciaoTJ
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdarg.h>
#include <typeinfo>
struct Exception : std::exception {
Exception (const char *format, ...) __attribute__((__format__ (__printf__, 2, 3)));
virtual const char* what() const throw() { return reason ? reason : "out of memory"; }
Exception (const Exception &e) : reason (e.reason ? strdup (e.reason) : NULL) {}
~Exception() throw() { if (reason) free (reason); }
private:
char *reason;
Exception& operator= (const Exception&);
};
Exception::Exception (const char *format, ...)
{
va_list args;
va_start (args, format);
reason = NULL;
if (asprintf (&reason, format, args) < 0 || !reason)
reason = NULL; /* indicate "out of memory" */
va_end (args);
}
struct Obj {
Obj ()
{
const char *thisname = typeid (this).name();
printf ("A: thisname: %s\n", thisname);
throw Exception ("%s: error", typeid (this).name());
throw Exception ("%s: error", thisname);
}
};
int
main (int argc,
char *argv[])
{
try {
Obj a;
} catch (std::exception &e) {
printf ("exception: what(): %s\n", e.what());
}
return 0;
}