Hey guys. I have a program that is crashing a lot shortly after a temporary object is deleted. This immediately flagged in my mind as a memory corruption issue since I have seen it before... but it is not giving any cryptic memory address errors (which I think it usually does when you have corrupted memory).
Does anyone have any suggestions about how to go about debugging this and finding the source of the crash? It is a large project so I won't even bother trying to copy it here.
Run your debugger. Then you can find out exactly which line is causing the error. If it is the destruction of the temporary object, then what is in it's constructor?
I don't have a debugger to my knowledge, or at least I don't know how to use it. I am programming in linux using g++ command line to compile my program.
I added the following to the part of my code that deletes the object...
... at which point the program crashes. So I suppose it must be an object corruption issue. I hate these errors as they dont cause the crash until you delete the object so you have no idea where the problem started -_-
In the constructor are numerous memset()s and variable initializations which I have double checked to ensure I was not memsetting past any array bounds or anything silly like that. Guess it's time to start hunting :'(
I think i have this working again, but it must have been an inheritence problem because it started working after I copied all of the functions from my baseClass and pasted them into both classes that inherited from my baseClass...
I have a base classes that two other classes inherit from:
// base class
class baseClass{
public:
// public members
private:
// private members
}
// Object 1
class object1: public baseClass{
public:
// public members
private:
// private members
}
// Object 2
class object2: public baseClass{
public:
// public members
private:
// private members
}
Out of curiosity is there anything fundamentally wrong with the following code? I.E. having some utility functions in my base class and then just using them in my child class when required? Because this is essentially what I was doing when my program was crashing. It stopped crashing after I moved my utility functions to the child classes.
class baseClass{
public:
baseClass();
~baseClass();
int getHour();
int getMinute();
}
class object1: public baseClass{
public:
object1(int a, char b, short c);
~object1();
int init(){
createdTime.hour = getHour();
createdTime.minute = getMinute();
}
void printCreationTime(){
printf("\n\nObject1 created at %u:%u.", createdTime.hour, creationTime.minute);
}
private:
struct creationTime{
int hour;
int minute;
};
creationTime *createdTime;
}
Missing virtual destructor on baseClass.
Missing copy semantics on object1.
Syntax error in object1::printCreationTime().
I hope object1's constructor intialises createdTime.
that's not what your single line of code shows - in C++, if you have no parameters, you don't use parenthesis when calling new - you obviously need parens if you have parameters
btw, heed kbw's observations carefully - if you ignore them, at best, you get a compile error. At worse, you will spend days debugging your code. If you don't understand why, better read some more books on C++ more carefully or ask questions. BTW, in C++, a pointer isn't automagically initialized to anything, so look at createdTime carefully.