Mail Archives: cygwin/1999/10/10/10:17:11
Hi Christophe,
It's a problem in your code.
Christophe Trophime wrote:
> ...
> class File_Exception{
> public :
> char * filename;
> char * status;
> File_Exception(const char *msg) {
> filename = new char [strlen(msg)+1];
> strcpy(filename, msg);
> status = NULL;
> };
> virtual ~File_Exception() {
> cout << "File_Exception Destructor : " << filename << " " << status
> << endl << flush;
> delete[] filename;
> delete[] status;
> };
> virtual void debug_print(){
> cerr << "File_Exception : ";
> cerr << filename << " " << status;
> cerr << endl << flush;
> };
> void set_status(const char * _thestatus){
> status = new char[strlen(_thestatus)+1];
> strcpy(status,_thestatus);
> };
>
> };
This class can not be used as an exception, because it contains dynamic
data members, but doesn't have an appropriate copy constructor.
> ...
> void init(){if (str->fail()) throw File_NotFound(filename);};
> ...
This code will create a temporary object of class File_NotFound and than
it will throw a copy of the object. Than, before your catch, the
temporary is destroyed. Because you have not defined a copy constructor,
the thrown objects contains garbage data members. I'm not sure if the
compiler is allowed to optimize away the copy by using the temporary
object directly, but you certainly can't count on this.
Avoid problems like this for not only exceptions but all classes, by
always either
- ensuring that the default copy constructor and assignment operator
work, or
- defining your own copy constructor and assignment operator or
- making the copy constructor and assignment operator private members of
the class, so that the compiler will give you a diagnostic if your code
tries to use them.
so long, benny
======================================
Benjamin Riefenstahl (benny AT crocodial DOT de)
Crocodial Communications GmbH
Ruhrstr. 61, D-22761 Hamburg, Germany
--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com
- Raw text -