Post subject: Gah, error when I exit Visual C++ program (pointers?)
Posted: Wed 02-02-2005 10:17PM
Colonel
Joined: Fri 08-22-2003 9:55AM Posts: 960
Source: TJ North
Alright, my program works fine up until I close it out. It then throws a fit that I presume to be a heap overflow, but I hardly know anything of the sort.
"Unhandled exception at 0x7c176cfa (mfc71d.dll) in demo2.exe: 0xC0000005: Access violation reading location 0xccccccc8." The error points to a line in Demo2.CPP, which is not even code I wrote.
Code:
if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel }
// Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE;
I assume I've got a problem involving pointers, but I'm so sketchy on the subject, I have no idea where to find it.
I'll pm my code to anybody that thinks they can help me out.
If you're running it through the Visual Studio debugger, after it's gone through the dialogs telling you it's crashed, but before you actually terminate it, do an Alt-7 to bring up the call stack. Look in there for functions that you've written or have code in. You can double click on a function in the call stack to jump to it and see its local variables.
Other than that, here are some basic ideas:
Make sure you call delete on every pointer that you've used new on.
If you've used new to allocate an array, like this:
int *p = NULL;
p = new int [10];
you must use delete like this:
delete [] p;
If you just say delete p; then Bad Things (tm) can happen.
Make sure you haven't walked off the end of an array somewhere. If you're using raw character pointers (char *'s and char []'s ) this can easily happen. Sometimes, walking off the end of an array doesn't produce an immediate crash; the crash happens when you return from the function you did this (called "smashing the stack").
Thanks for the input, but that didn't seem to do the trick. For some reason it won't let me put the destructor in the functions.cpp file, so I have to put it in the class definition header file. I don't think this would solve the problem but still a strange occurence.
I get this error:
Linking...
payroll.obj : error LNK2005: "public: __thiscall Payroll::~Payroll(void)" (??1Payroll@@QAE@XZ) already defined in demo2.obj
Debug/demo2.exe : fatal error LNK1169: one or more multiply defined symbols found
I don't have it defined twice, so no idea where thats coming from.
Thanks for the input, but that didn't seem to do the trick. For some reason it won't let me put the destructor in the functions.cpp file, so I have to put it in the class definition header file. I don't think this would solve the problem but still a strange occurence.
I get this error: Linking... payroll.obj : error LNK2005: "public: __thiscall Payroll::~Payroll(void)" (??1Payroll@@QAE@XZ) already defined in demo2.obj Debug/demo2.exe : fatal error LNK1169: one or more multiply defined symbols found
I don't have it defined twice, so no idea where thats coming from.
check your includes.
_________________ chem^ten
a.k.a hardcore
03-04 4NW RA
Thanks for the input, but that didn't seem to do the trick. For some reason it won't let me put the destructor in the functions.cpp file, so I have to put it in the class definition header file. I don't think this would solve the problem but still a strange occurence.
I get this error: Linking... payroll.obj : error LNK2005: "public: __thiscall Payroll::~Payroll(void)" (??1Payroll@@QAE@XZ) already defined in demo2.obj Debug/demo2.exe : fatal error LNK1169: one or more multiply defined symbols found
I don't have it defined twice, so no idea where thats coming from.
If you're defining it in your .h file, try putting the keyword
inline
before the definition.
Something like...
class Demo
{
public:
Demo();
~Demo();
//etc. etc.
};
inline
Demo::~Demo()
{
//destructor code goes here
}
You can also do something like
class Demo
{
public:
Demo();
~Demo()
{
//destructor code goes here
//note that when you define functions this way the semicolon goes after
//the curly brace that ends the function. No semicolon goes after the
// "~Demo()"
};
};
However, from a style standpoint, when you have inline functions it's usually better to put them at the end of the .h file (using the first tecnique). The reason is that the class declaration's purpose for humans is to say what the class does. The definitions' purpose is to say how the class does it. Good C++ coding standards seperate what a class does from how the class does it. Said another way, it seperates the interface from the implementation.
thanks for all the help guys. I managed to work the problem out. I scrapped the program and rewrote everything after looking at that error for more than 3 hours. Kinda sucks that I wasted so much time on it, but oh well, I should now at least be able to get a grade. Thanks again.
Joined: Thu 06-26-2003 6:38PM Posts: 449 Location: The Kablaamdom
Source: Kelly Hall
midgey wrote:
thanks for all the help guys. I managed to work the problem out. I scrapped the program and rewrote everything after looking at that error for more than 3 hours. Kinda sucks that I wasted so much time on it, but oh well, I should now at least be able to get a grade. Thanks again.
It's odd how often that works... but I generally just start from the top as if I was coding it rather than retyping everything. Heh.
Users browsing this forum: No registered users and 2 guests
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum