Crash normally, but not when debugging ( SDL, malloc )

Ok, this is doing my head in...
Notes: Im Using SDL, Im using Codeblocks to code

Ive narrowed down my crashing problem ( using printf's ) to a malloc call
I had to use printf's because when i ran the program in Codeblocks debugging mode, it did not crash and ran fine, but when i ran it normally, it would crash, giving me this error:

fatal signal segmentation fault (sdl parachute deployed)

Inside my code, I created a malloce function that checks malloc for me ( so i dont have to do it )

( general.h )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void* malloce ( size_t size )
{
	printf ( "start malloce %i\n", size );
	void* pointer = malloc ( size ); //<------apparent crash area

	printf ( "before check\n" );

	if ( pointer == NULL )
	{
		printf ( "\n\n|-[]= OUT OF MEMORY =[]-|\n\n" );
		exit ( 3 );
	}
		printf ( "end malloce\n" );
	return pointer;
}


And this is where im calling it from

( employee.h )
employee* emp = malloce ( sizeof ( employee ) );

funny thing is that i called this malloce function about 20 times before it hits this part of the code, it is unknown behavior to me!

All help is much appreciated!

many thanks, superstinger
If your program is crashing when you access heap functions, that means you've corrupted the heap earlier with a memory bounds overwrite.

So don't look for the problem there, double check how you've handled memory coming from the heap earlier in your program.

This is the sort of problem than memory checkers like valgrind can help with.
Last edited on
hmm im using windows, so valgrind wont be so good...
any other programs that would be good for windows?

and how can i create a 'memory bounds overwrite'?

What am i to look for in earlier places of my code?
Last edited on
The Microsoft C++ debug environment has heap checking tools in there somewhere; it's enabled for MFC apps in debug mode.

I made a post some time back on how to enable it for your own apps, but I can never find anything in this forum; but it's here somewhere.
Last edited on
This thread might be of some help:

How to debug heap corruption errors?
http://stackoverflow.com/questions/1010106/how-to-debug-heap-corruption-errors

I have a feeling that Application Verifier might be only available with the paid for versions of Visual Studio, but I am unsure.

Application Verifier
http://msdn.microsoft.com/en-us/library/windows/desktop/dd371695%28v=vs.85%29.aspx

Download Microsoft Application Verifier
http://www.microsoft.com/en-us/download/details.aspx?id=20028

But you can You can also use gflags.exe to enable the debug heap. This tool comes as part of the Debugging Tools for Windows

And then there's the Debug CRT heap, which I think is what kbw was referring to (as it works with MFC):

The CRT Debug Heap
http://msdn.microsoft.com/en-us/library/974tc9t1%28v=vs.100%29.aspx

Debug Heap Features
http://msdn.microsoft.com/en-US/library/wxex5fzd%28v=vs.100%29.aspx

Inc. _CrtCheckMemory, which can be used to "check the heap's integrity at any point.".

Andy

PS Searching cplusplus.com for _CrtCheckMemory find this thread from May 2012, but nothing else: Heap Corruption
http://www.cplusplus.com/forum/beginner/70535/
Last edited on
> employee* emp = malloce ( sizeof ( employee ) );
Give me one good reason to do that instead of simply employee emp;
@ne555
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
employee* createEmployee ()
{


	employee* emp = malloce ( sizeof ( employee ) );



	//Initialise vars
	int i;
	for ( i = 0; i < 7; i++ )
	{
		emp->days[i].startTime = 0;
		emp->days[i].finishTime = 0;
	}

	emp->role = -1;
	emp->placement = -1;
	emp->status = 0;
	//This will be malloce'd later in other functions
	emp->firstName = NULL;
	emp->lastName = NULL;

	return emp;
}


this is the code used for creating an employee data structure

The code below is in main

1
2
3
4
5
6
	employee* allEmployees[MAX_LIST];

	for ( i = 0; i < MAX_LIST; i++ )
	{
		allEmployees[i] = NULL;
	}


The reason i have used malloc is because i start off with an empty list of employees, i do not know how many employees the users wants to create, it could be 10, 100 or 1000 if they want, i do not want to make '1000 employees' right off the bat, might be a waste of space :P. If there is a better way of doing this, let me know, thanks ne555

@kbw
thanks for your help along the way, tips got me started in the right direction

@andywestken
Thanks for these handy programs, they will sure come in handy in finding those pesky memory leaks and heap corruptions!

@EVERYONE
I found the heap corrupting code

1
2
3
4
5
6
7
8
9
10
	char* empRead = malloce ( ( sizeof ( char ) * length ) + 1 );// <------
	rewind ( fp );

	//Now read the file into memory
	int i;
	for ( i = 0; i < length; i++ )
	{
		empRead[i] = fgetc ( fp );
	}
	empRead[i] = '\0';//<------ 


The '+ 1' was not there before ( indicated by the first arrow is pointing in comments )
which caused empRead to overwrite one byte where it shouldnt have ( indicated by the second arrow )
Last edited on
Last edited on
@ne555

I made a similar reply, then deleted it, because the OP is doing SDL, which apparently uses C.

I have no idea if there are C++ wrapper libraries for SDL or not, or whether it would be worth it for the OP to write their own.

regards
You don't need wrapper libraries to use SDL in c++
@ne555

OK thanks for that, I really don't know anything about SDL.
Topic archived. No new replies allowed.