Problem inheriting from std::exception

Denis Vakatov vakatov@ncbi.nlm.nih.gov
Fri Feb 23 22:28:00 GMT 2001


I believe it's because of the way GCC delivers the exception (or at least
used to deliver it yet several months ago).

It actually delivers a *copy* of the original exception, and not the original.

As I can see in your code, you don't have copy constructor defined,
and therefore the default copy constructor generated by GCC will apparently
make a binary copy of your "file_" member pointer. Then, destructor of
original exception will deallocate "file_", and then destructor of the
copy of this exception will try to deallocate just the same pointer
(well, most probably, you'd try to use this pointer even before the destructor,
when accessing "file_" through "file()", and using it for e.g. diagnostics)...

To prove this, try to put a breakpoint on the destructor, and see
what happens.

Other compilers (like your Digital one) may (and often do) deliver
exceptions in a different way, sometimes without making extra copy(es)
of the original exception object... that's why it may be okay with
e.g. Digital. However, it might be that Digital just did not manage
to corrupt the deallocated memory block by the time you tried to use it,
and that it "graciously" allowed double deallocation of the "file_"
pointed memory, too. :-)

Hope it helps,
Denis


---------------------------------
Problem inheriting from std::exception

    To: libstdc++ at gcc dot gnu dot org 
    Subject: Problem inheriting from std::exception 
    From: Craig Rodrigues <rodrigc at mediaone dot net> 
    Date: Sat, 17 Feb 2001 21:09:07 -0500 



Hi,

I took a look at this bug report in GNATS:
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=1900&database=gcc

I modified the example so that it would compile with gcc 2.97:

=======================================================================
 
 #define DEBUG
 
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <string>
#include <iostream>
 
namespace gutils {
 
  class Exception : public std::exception
  {
  public:
     Exception
     (
      const std::string& what_arg,
      const char* file = 0,
      unsigned long line = 0
      ) : e_what_(what_arg), file_(0), line_(line)
    {
      if (file)
        file_ = new std::string(file);
    }
 
    virtual ~Exception() throw () { delete file_; }
 
    virtual const char* what() const throw() { return e_what_.c_str(); }
 
    virtual const char* file() const
    {
      if (file_)                                         
        return file_->c_str();
 
      return 0;
    }
 
    virtual unsigned long line() const { return line_; }
 
  private:
    std::string e_what_;
    std::string* file_;
    unsigned long line_;
  }; // class Exception
 
 #ifdef DEBUG
 #define EXCEPTION(WHAT) gutils::Exception((WHAT), __FILE__, __LINE__)
 #else
 #define EXCEPTION(WHAT) gutils::Exception((WHAT))
 #endif
  class LogicError : public std::logic_error, public gutils::Exception
  {
  public:
     LogicError
     (
      const std::string& what_arg,
      const char* file = 0,
      unsigned long line = 0
      ) : logic_error(what_arg), Exception(what_arg, file, line) {}
 
    virtual const char* what() const throw() { return Exception::what(); }
 
    // virtual ~LogicError() { }
  };
 
 #ifdef DEBUG
 #define LOGIC_ERROR(WHAT) gutils::LogicError((WHAT), __FILE__, __LINE__)
 
 #else
 #define LOGIC_ERROR(WHAT) gutils::LogicError((WHAT))
 #endif
 
}; // namespace gutils                

==============================================================================

If I compile this code with gcc 2.97, it coredumps when I try to run it.
When I compiled it with the Digital Unix C++ compiler, it compiled
and ran fine.
Is this a bug in the code, or a bug in libstdc++-v3?
I am not familiar enough with the Standard
to track this bug down.

Any help would be appreciated.
-- 
Craig Rodrigues        
http://www.gis.net/~craigr    
rodrigc@mediaone.net          



More information about the Libstdc++ mailing list