[Cppunit-devel] faq question about ExceptionTest.testAssignment
Brought to you by:
blep
From: Greg Z. <gr...@pa...> - 2001-10-19 08:14:26
|
Hi, In response to the faq question Why does the test ExceptionTest.testAssignment failed in CppUnit test suite? I don't know if anyone has responded to this bug yet but here is what I discovered. As best as I can tell when cppunit Excepiton::operator= calls SuperClass::operator= (which really is std::exception::operator=), the cppunit Exception object's vtable address is overwritten with that of std::exception's vtable address. So when you call the what() method (a call through the vtable since it is virtual), instead of calling cppunit Exception's what(), it calls std::exception's what. This returns the value stored in std::exception's _m_what member, which is "Unknown exception" (the default value). So either there is a bug in std::exception operator= or the compiler is not properly setting up the vtable when calling std::exception operator=. The copy constructor works fine. You can fix the bug by either - Don't have cppunit's class Exception inherit from std::exception and remove all use of std::exception. NOTE: I tried this and it worked fine. I.e. ExceptionTest.testAssignment passed. Not really sure why you are inheriting from std::exception since nothing in it is really used. or - Still have cppunit's class Exception inherit from std::exception and use it's _m_what member instead of cppunit Exception's m_message. You need to make these changes * add std::exception initialization to the following cppunit Exception constructor Exception::Exception( std::string message, long lineNumber, std::string fileName ) : std::exception ( message.c_str() ), <=== add this to initialize std::exception's _m_what member // m_message( message ), <== remove this not needed any more. using std::exception to hold the message m_lineNumber( lineNumber ), m_fileName( fileName ) { } * remove m_message and all it's uses * remove the what() method from exception.h and it's implementation from exception.cpp. You can still call what() as it will invoke std::exception::what() which will return the value in _m_what as a const char *. NOTE: I tried this and it worked fine. I.e. ExceptionTest.testAssignment passed. However, the problem with this is that the vtable address for cppunit Exception is wrong if operator= is ever called. Could lead to problems down the road. or - Leave everything as is and get MS to fix their bug. ~greg |