Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / fstream memory violation

Author
Message
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 31st May 2012 00:57
I'm not entirely sure if this is the correct place to be posting things like this as it's purely C++ related. Do we have a C++ forum?

Anyway, I have here a debugging class.

CDebugSystem.h


CDebugSystem.cpp


And here is how it's implemented:



The problem is in the file "CDebugSystem.cpp" where I try to write to the file (I highlighted the line with a comment above). I've debugged the code to check that the filename is passed correctly - which it is - and I can confirm that the actual file "test.txt" appears on my hard drive. I then made sure that the text I'm writing to it is passed correctly - which it is - so now I'm kind of lost as to why File << Message << endl; doesn't work...

I even tried replacing that line with File << "test" << endl, but no success.

TheComet

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 31st May 2012 01:31 Edited at: 31st May 2012 01:41
You shouldn't be using "new" and "delete". TBH, I'm surprise that "delete File" even compiled because "File" isn't a pointer type.

If you get rid of the line:
ofstream* File = new ofstream;
Which creates a new local variable unrelated to the File member in the class.

And also the line:
delete File;

Then it should work.

In general you don't need to use "new" and "delete" anymore for normal programming in newer versions of C++. Objects should usually be created on the stack or incorporated directly into classes, and in the cases where that can't be done it's usually best to use a "std::shared_ptr<T>" and "std::make_shared()" to allocate your objects. These are fast, exception safe and use reference counting so that you won't leak memory.

In VS2008, shared_ptr is in "std::tr1" and "make_shared" doesn't exist, you just use the shared_ptr constructor instead.

[b]
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Jun 2012 16:15
Quote: "I'm surprise that "delete File" even compiled because "File" isn't a pointer type."


Ugh, that was stupid of me, I'm a little rusty but I think it's coming back to me now.

The reason why I chose to use "new" is so the object isn't destructed when going out of scope (so I can stream data to the file later on without it being destructed once the program leaves the CDebugSystem() constructor). I found a better debugging library anyway, so I'll replace the one I created with the new one.

Thanks for the help!

TheComet

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 2nd Jun 2012 16:56
Quote: "The reason why I chose to use "new" is so the object isn't destructed when going out of scope (so I can stream data to the file later on without it being destructed once the program leaves the CDebugSystem() constructor)."


Since File is a member of the class it won't be destructed until the destructor for the class is called anyway.

[b]
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 2nd Jun 2012 19:15
Quote: "Since File is a member of the class it won't be destructed until the destructor for the class is called anyway."


Ah, I hadn't thought of that. Thanks!

TheComet

Login to post a reply

Server time is: 2024-04-26 22:37:58
Your offset time is: 2024-04-26 22:37:58