Skip to main content
gamedev.net
🔒 Locked

Error Log Help

Started by Crusable77 18 replies 2.1k views
Original Post
Crusable77
Crusable77
Hello,

I am learning SDL and i thought it would be helpful to make an Debug log to tell me if something is working or not. I am doing the project as a stand alone just to see if it works and then implement it in my SDL projects. So i have played a round with it a little but i cannot get the program to work. I am trying to make a class that handles all the debug stuff for me using the fstream to make a file telling me what happened. I cannot seem to get this to work, can anyone give me some hints? anything would be much appreciated.
Bacterius
Bacterius
Let me just grab my crystal ball... seriously, if you want help on your code, you might want to post said code, otherwise there's not much to go on. We're not psychic tongue.png
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Crusable77
Crusable77
Sorry i thought i linked the project.
Main.cpp
[source lang="cpp"]#include "stdafx.h"
#include "DebugLog.h"
int main (){
DebugLog debug("DebugLog.txt");
debug.~debug();
return 0;
}[/source]
DebugLog.h
[source lang="cpp"]
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

class DebugLog {
public:
DebugLog();
~DebugLog();
string writeTo(string text);
private:
string m_Text;
string m_Filename;
};[/source]
DebugLog.cpp
[source lang="cpp"]
#include "stdafx.h"
#include "DebugLog.h"
DebugLog::DebugLog(){
ofstream debugLog("DebugLog.txt");
m_Text = "-----Debug Log Starts Now-----\n\n";
debugLog << m_Text;
}
DebugLog::~DebugLog(){
m_Text = "-----End of DebugLog-----";
debugLog << m_Text;
debugLog.close();
}
string DebugLog::writeTo(string text){
m_Text = text;
debugLog << text;
return;
}[/source]
Crusable77
Crusable77
I dont know whats wrong the code isnt right and the site wont let me reply properly.
Bacterius
Bacterius
Try pastebin.com, the code tags aren't working too well at the moment
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Crusable77
Crusable77
DebugLog.h:
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

class DebugLog {
public:
DebugLog();
~DebugLog();
string writeTo(string text);
private:
string m_Text;
string m_Filename;
};
Debug.cpp:
#include "stdafx.h"
#include "DebugLog.h"
DebugLog::DebugLog(){
ofstream debugLog("DebugLog.txt");
m_Text = "-----Debug Log Starts Now-----\n\n";
debugLog << m_Text;
}
DebugLog::~DebugLog(){
m_Text = "-----End of DebugLog-----";
debugLog << m_Text;
debugLog.close();
}
string DebugLog::writeTo(string text){
m_Text = text;
debugLog << text;
return;
}
and Main.cpp
{
DebugLog debug("DebugLog.txt");
debug.writeTo("Line 1\n");
debug.~debug();
return 0;

I have an idea about whats going on but not a way to fix it.
Bacterius
Bacterius
First, don't call destructors like that. Use delete debug instead - and if the class is allocated on the stack instead - like it is here - just let it die when it goes out of scope, its destructor will be called automatically.

Secondly, does this even compile? debugLog is not defined in the destructor or in writeTo(). Basically, you allocate your iostream in the constructor, which is on the stack - as soon as it goes out of scope (i.e. when the constructor returns) the iostream is closed and lost. This iostream exists only in the constructor. If you want to keep a persistent iostream throughout your debug log's lifetime, you want it to exist as a private field in your class. Then you can use it anywhere, and your writeTo() will actually work. Does it make sense?
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Crusable77
Crusable77
If im understanding this right, your saying the ofstream debugLog("DebugLog.txt"); should be in my private: part of the class. When i do this i get an error saying that the "DebugLog.txt" needs an identifier before it. And Im not understanding what your saying about the destructor.
Crusable77
Crusable77
And int the main function it says DebugLog debug("DebugLog.txt"), that isnt supposed to be there sorry i forgot to delete it.
Bacterius
Bacterius
[source lang="cpp"]class DebugLog {
public:
DebugLog();
~DebugLog();
string writeTo(string text);
private:
string m_Text;
string m_Filename;
ofstream m_debugLog;
};

DebugLog::DebugLog()
{
m_debugLog.open("DebugLog.txt"); // m_debuglog now belongs to the class, not just the constructor
// so it can be accessed in writeTo() as well
}[/source]

This means the debugLog object is now attached to your class instead of a specific method, so every method in the class has now access to it. The constructor takes care of creating the stream. Does it make more sense now?

Never mind the destructor stuff for now - you need to understand the concept of class fields before.
“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
Crusable77
Crusable77
Yes it makes sense now, thanks. But when i compiles it a message poped up saying Debug Assertion failed, what does this mean?
Crusable77
Crusable77
Nevermind, i fixed it, but thank you for the help.
Crusable77
Crusable77
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.
Bacterius
Bacterius
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.”
Crusable77
Crusable77
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?
Crusable77
Crusable77
constructor not compiler, and im sorry im posting so much but it wont let me edit my posts
Bacterius
Bacterius
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.”
Crusable77
Crusable77
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.
Bacterius
Bacterius
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.”
Crusable77
Crusable77
Thanks for your help

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.