I added a new class to a dll which is part of solution. All was working fine until I started getting error discussed in this question when I would start the solution with debugger. This means that debugger was catching something bad, probably heap corruption although it was not pointing it out exactly where.
I narrowed down the issue to my added class, if I remove it, the solution debugs fine, no errors. But apparently, there is nothing wrong with the class I added!
So I commented my class and added a new simple (dummy) class Book
just to see if it will produce the same error but it don't! Then I made it look similar to my added class (derived from ofstream
) and still it will debug fine, no errors.
I commented it out and uncommented my original class, again the error appears when I debug it! I skimmed this class and left only constructor/destructor now I can debug it, the error goes away here too. I brought back delete code (using undo so to restored exactly) and this time it works (when it was not before) and will debug fine without throwing that corruption error!
So this sounds a lot like undefined behavior but this is a very lightweight standalone class which is instantiated correctly so is undefined behavior still the suspect? If so, why would it cause this behavior in this case?
Here are my .h and .cpp files for reference
class lfstream : public std::ofstream
{
public:
lfstream();
~lfstream();
void log(const std::string &text, int threadID);
};
#ifdef _LOGDLL
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif
API extern lfstream logstream;
/*
class Book : public std::ofstream
{
public:
Book();
~Book();
int WordCount();
};
API extern Book book;
*/
The .cpp file
static char logfname[] = "debug.txt";
lfstream::lfstream(): std::ofstream( logfname )
{
}
lfstream::~lfstream()
{
}
void lfstream::log(const std::string &text, int threadID)
{
const time_t ctt = time(0);
*this << std::setw(40) << std::left << text << " thread id = " << threadID << "\t" << asctime(localtime(&ctt)); // << std::endl;
}
lfstream logstream;
/*
Book::Book() : std::ofstream("crash.txt")
{
}
Book::~Book()
{
}
int Book::WordCount()
{
return 50;
}
Book book;
*/