Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT sourceware DOT cygnus DOT com Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com Message-Id: <3800A169.67C16C4D@crocodial.de> Date: Sun, 10 Oct 1999 16:23:37 +0200 From: Benjamin Riefenstahl Organization: Crocodial Communications EntwicklungsGmbH X-Mailer: Mozilla 4.05 [en] (Win16; I) MIME-Version: 1.0 To: cygwin AT sourceware DOT cygnus DOT com Subject: Re: problem with exceptions on egcs1.1.2 References: <37FB2543 DOT AF3F9542 AT polycnrs-gre DOT fr> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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