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::WinapiException(const std::string & extraInfo)
:_extraInfo(extraInfo)
{
// Nothing
}
The problem is the what() method. Mine looks like this:
const char* WinapiException::what() const throw()
{
std::string rc;
rc = typeid(this).name();
// errorString formats the winapi error message and returns an std::string
rc += "GetLastError reports:\n" + errorString(::GetLastError());
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:-)