Program crashes after object is deleted

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.

Thanks in advance :)
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...

1
2
3
4
5
6
7
8
printf("\n\n");
printf("\n\nAbout to delete object...");
printf("\n\n");
delete d;
printf("\n\n");
printf("\n\nFinished deleting object.");
printf("\n\n");


.. and the only output I get is as follows...
 
About to delete 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 :'(
Last edited on
Tried valgrind yet?
http://valgrind.org/
Make sure that the pointer you are deleting contains a valid address( which you have assigned, i mean by dynamic mem.. )


_______
Bandit
try gdb on the command-line:

http://www.cs.cmu.edu/~gilpin/tutorial/

but make sure you are passing -g to your g++ command (eg g++ -g -o junk junk.cpp)
I perform the following check to ensure the pointer is valid before continuing:

1
2
3
4
object *d = 0;
d = new object();
if(d == 0) return;
// else continue 


Also calls to the objects member functions behave as expected indicating the object is working to a marginal stability.

I have used Valgrind before but I cannot get Valgrind for the version of Linux I am using for this program as it is very old.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 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
}


I have no idea why though.
Last edited on
make sure you have no parens after object on Line 2, when you are allocating using new
@ kfmfe...

Why would I not put parenthesis when allocating using new? My constructor takes arguments and I need to pass in arguments like this...


 
d = new object2(int a, char b, short c);
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;
}
Last edited on
Missing virtual destructor on baseClass.
Missing copy semantics on object1.
Syntax error in object1::printCreationTime().
I hope object1's constructor intialises createdTime.
why is createdTime a pointer?
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.
Use gdb.

You are programming in Linux enviroment, so do I. Sooner or later, you have to use GDB in your way to a professional programmer. :)



b2ee
Topic archived. No new replies allowed.