• Advertisement
Sign in to follow this  

Error Log Help

This topic is 1915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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 [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
And int the main function it says DebugLog debug("DebugLog.txt"), that isnt supposed to be there sorry i forgot to delete it.

Share this post


Link to post
Share on other sites
[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. Edited by Bacterius

Share this post


Link to post
Share on other sites
Yes it makes sense now, thanks. But when i compiles it a message poped up saying Debug Assertion failed, what does this mean?

Share this post


Link to post
Share on other sites
Sorry, one more thing, When i compile it i get a message that says

:[b]Unhandled exception at 0x7784ef10 in DebugLog.exe: 0xC0000005: Access violation reading location 0xfeeefef6.[/b]

Share this post


Link to post
Share on other sites
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.

[quote]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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

[b]Remove all the extra spaces I had to insert since the source tags are broken.[/b] I think we're just having a communication breakdown here. Edited by Bacterius

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement