delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/10/10/10:17:11

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT sourceware DOT cygnus DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT sourceware DOT cygnus DOT com>
List-Archive: <http://sourceware.cygnus.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sourceware DOT cygnus DOT com>
List-Help: <mailto:cygwin-help AT sourceware DOT cygnus DOT com>, <http://sourceware.cygnus.com/ml/#faqs>
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 <benny AT crocodial DOT de>
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>

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019