Hi,
I have an exception class for winapi errors (returns a formated error mesage,
usign ::GetLastError( ) et al, with what()).
It looks like this:
class WinapiException : public std::exception
{
public:
WinapiException (const std::string & extraInfo);
virtual const char* what() const throw();
private:
std::string _extraInfo;
};
WinapiException ::WinapiExcepti on(const std::string & extraInfo)
:_extraInfo(ext raInfo)
{
// Nothing
}
The problem is the what() method. Mine looks like this:
const char* WinapiException ::what() const throw()
{
std::string rc;
rc = typeid(this).na me();
// errorString formats the winapi error message and returns an std::string
rc += "GetLastErr or reports:\n" + errorString(::G etLastError());
if( _extraInfo.size () > 0)
{
rc += "\nExtra Info:\n" + _extraInfo;
}
else
{
rc += "\nNo extra info available.";
}
return rc.c_str(); // here's the problem!!!
}
The what() returns a const char* that points to something that has already
been destroyed and freed.
What's the best solution for this? O:-)