Error Log Help

Started by
18 comments, last by Crusable77 11 years, 6 months ago
Yes it makes sense now, thanks. But when i compiles it a message poped up saying Debug Assertion failed, what does this mean?
Advertisement
Nevermind, i fixed it, but thank you for the help.
Sorry, one more thing, When i compile it i get a message that says

:Unhandled exception at 0x7784ef10 in DebugLog.exe: 0xC0000005: Access violation reading location 0xfeeefef6.
Whole code, please. Works fine here. You're trying to access 0xfeeefeee, which is a bit pattern written by the Microsoft compiler to indicate memory that has been freed, So either you're not calling the constructor, either you're trying to use the DebugLog after you've freed it.

And int the main function it says DebugLog debug("DebugLog.txt"), that isnt supposed to be there sorry i forgot to delete it.[/quote]
What did you mean by that?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

In the code i posted above it said in the main function debugLog debug("DebugLog.txt"). I sending the "DebugLog.txt" to the compiler so it name the file, but i changed it to name it in the constructor itself because i wasn't planning on changing the name. And what do you mean by using the Constructor after i freed it and why would it work for you and not for me?
constructor not compiler, and im sorry im posting so much but it wont let me edit my posts
Sanity check: does this work for you?

[source lang="cpp"]#include < iostream >
#include < fstream >

using namespace std;

class DebugLog {
public:
DebugLog();
void writeTo(string text);
~DebugLog();
private:
ofstream m_debugLog;
};

DebugLog::DebugLog()
{
m_debugLog.open("DebugLog.txt");
}

DebugLog::~DebugLog()
{
cout < < "Closing debug log." < < endl;
m_debugLog.close();
}

void DebugLog::writeTo(string text)
{
m_debugLog < < text;
}

int main()
{
DebugLog log;
log.writeTo("hello world");

// log is about to fall out of scope - destructor should be called
return 0;
}
[/source]

Remove all the extra spaces I had to insert since the source tags are broken. I think we're just having a communication breakdown here.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

My code does the same thing yours does, however mine has different names, and when i deleted the Destructor i didnt get the message i got before.
Yes, you must essentially never call a destructor using the tilde method. If you need to delete it, use the "delete" keyword, and that's only for pointers (e.g. when going DebugLog* log = new DebugLog(), but that's for later). In your case, the destructor will be automatically called (since once you go out of main, your DebugLog is lost and can never be used again, so C++ chooses to destroy it there).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Thanks for your help

This topic is closed to new replies.

Advertisement