Advertising (This ad goes away for registered users. You can Login or Register)

Heap error

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
electrosheep
Posts: 97
Joined: Tue Jan 11, 2011 2:50 am

Heap error

Post by electrosheep » Thu Sep 15, 2011 9:49 pm

The /talk forums have been overhauled since last I was here.

I am getting a strange error when I attempt to delete some memory.

The background is that I had a function that would draw a menu in the same place every time. I thought to myself, "Self, I think I should overload this bad boy, so that I can draw the menu wherever I want".
So I drew up the new function, mostly be copy pasting what I already had, which was fully functional. To my dismay, I discovered that when I deleted the my dynamic memory, it caused my program to crash, saying:

Debug Error!

HEAP CORRUPTION DETECTED: after Normal block (#161) at
0x003E5858
CRT detected that the application wrote to memory after end of heap
buffer.

I wasn't aware that delete even wrote to memory, but apparently it does.

The reason I believe it to be the delete command is because I set up break points in my program, and it didn't crash before it executed that operation, but as soon as it did, crash.

Anyway, enough of this, here's the code

My original function that works perfectly

Code: Select all

int printMenu(string m[], int index){
	// My custom code, made to list index amount of menu items, instead of always 5
	int i, xpos = 10, *ypos;
	ypos = new int[index];
	for (int a = 1; a <= index; a++)
		ypos[a-1] = a * 3;
	
    // list the menu
    for (i=0; i< index; ++i){
        gotoxy(xpos, ypos[i] );
        textattr(0x0F);
        cout << m[i];
    }
 
    // make menu available to choose
    i=0;
    while(true){
		gotoxy(xpos, ypos[i]);
		textattr(0xF0);
		// probably not the best place to put this, but first hex number indicates background color
		// next hex number indicates text color
		cout << m[i];
 
        /* note : 72 -> UP button
            75 -> RIGHT button
            77 -> LEFT button
            80 -> DOWN button
        */
 
		switch(_getch()){
		case 'q':
		case 27:
			delete [] ypos;
			textattr(0x0F);
			return -1;
		case '8':
			if(i>0){
				gotoxy(xpos,ypos[i] );
				textattr(0x0F);
				cout << m[i];
				--i;
			}
			break;
 
		case '2':
			if(i< index-1){
				gotoxy(xpos,ypos[i] );
				textattr(0x0F);
				cout << m[i];
				++i;
			}
			break;
 
			// 13 is the enter key
		case 13:
			delete [] ypos;
			textattr(0x0F);
			return i;
		}
	}
}
My overloaded function that does crash

Code: Select all

// overloaded version of the above function, used to draw the menu
//  to a specific place. The delete command causes a heap error
int printMenu(string m[], int index, int x, int y){
	// My custom code, made to list index amount of menu items, instead of always 5
	int i, *ypos;
	ypos = new int[index];
	ypos[0] = y;
	for (int a = 1; a <= index; a++)
		ypos[a] = y + (a * 3);
	
    // list the menu
    for (i=0; i< index; i++){
        gotoxy(x, ypos[i]);
        textattr(0x0F);
        cout << m[i];
    }
    
    i=0;
    while(true){
		gotoxy(x, ypos[i]);
		textattr(0xF0);
		// probably not the best place to put this, but first hex number indicates background color
		// next hex number indicates text color
		cout << m[i];
 
        /* note : 72 -> UP button
            75 -> RIGHT button
            77 -> LEFT button
            80 -> DOWN button
        */
 
		switch(_getch()){
		case 'q':
		case 'Q':
		case 27:
			//delete [] ypos;
			textattr(0x0F);
			return -1;
		case '8':
			if(i>0){
				gotoxy(x,ypos[i] );
				textattr(0x0F);
				cout << m[i];
				--i;
			}
			break;
 
		case '2':
			if(i< index-1){
				gotoxy(x,ypos[i] );
				textattr(0x0F);
				cout << m[i];
				++i;
			}
			break;
 
			// 13 is the enter key
		case 13:
			// for some reason, deleting ypos in this function causes a crash
			delete [] ypos;
			textattr(0x0F);
			return i;
		}
	}
}
If any of the many gurus floating around on this site could take a look at that and help me fix it, I would be most grateful.
Advertising
Let me see...your grandmother's name was Beatrice?

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Heap error

Post by m0skit0 » Fri Sep 16, 2011 9:55 am

You call a function "overloaded" when both versions exist. In your case you've modified the function, and not overloaded it.

Is the amount of memory you're allocating the same on both codes? That is, is index having the same value?

PS: on a side note, I really don't like that C++ C-like coding. In C++ all functions should be inside a class, and not floating in nowhere. Mixing C++ and C like this looks so bad...
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: Heap error

Post by JJS » Fri Sep 16, 2011 11:10 am

I guess this is the problem:

Code: Select all

 ypos = new int[index];
   ypos[0] = y;
   for (int a = 1; a <= index; a++)
      ypos[a] = y + (a * 3);
You are writing one item beyond the upper array bound. Probably the delete operator checks for corrupted memory so it is only noticed there.

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Heap error

Post by m0skit0 » Fri Sep 16, 2011 11:32 am

True, didn't see that. Then both functions are wrong, so why the exception is not raised on the previous function?
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

JJS
Big Beholder
Posts: 1416
Joined: Mon Sep 27, 2010 2:18 pm
Contact:

Re: Heap error

Post by JJS » Fri Sep 16, 2011 12:34 pm

In the first function one is subtracted from the array subscript, no problem there:

Code: Select all

ypos[a-1] = a * 3;

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: Heap error

Post by m0skit0 » Fri Sep 16, 2011 12:52 pm

Right. Also in the second one it should be from 0 to index - 1 :

Code: Select all

ypos = new int[index];
   ypos[0] = y;
   for (int a = 0; a < index; a++)
      ypos[a] = y + (a * 3);
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

electrosheep
Posts: 97
Joined: Tue Jan 11, 2011 2:50 am

Re: Heap error

Post by electrosheep » Fri Sep 16, 2011 6:13 pm

Awesome! I don't know why I couldn't figure that out. I think the fact that one was working and one wasn't threw me off.

You guys should probably start charging people for your time.

EDIT: @m0skit0 both functions did exist, sorry I didn't make that clear. I now have four functions called printMenu, taking up so many lines of code I put them (and some other stuff) in their own header file.

Code: Select all

#ifndef _MENUS_
#define _MENUS_
#endif

#ifndef _ROUGEHEADER_
#include "RogueHeader.h"
#endif

#ifndef _CONSOLEMANIP_
#include "ConsoleManip.h"
#endif

#ifndef _MAP_
#include "Map.h"
#endif

#ifndef _CONIO_
#include <conio.h>
#endif

// bulk of function was taken from somewhere off the internet
int printMenu(string m[], int index){
	// My custom code, made to list index amount of menu items, instead of always 5
	int i, xpos = 10, *ypos;
	ypos = new int[index];
	for (int a = 0; a < index; a++)
		ypos[a] = a * 3;
	
    // list the menu
    for (i=0; i< index; ++i){
        gotoxy(xpos, ypos[i] );
        textColor(0x0F);
        cout << m[i];
    }
 
    // make menu available to choose
    i=0;
    while(true){
		gotoxy(xpos, ypos[i]);
		textColor(0xF0);
		// probably not the best place to put this, but first hex number indicates background color
		// next hex number indicates text color
		cout << m[i];
 
        /* note : 72 -> UP button
            75 -> RIGHT button
            77 -> LEFT button
            80 -> DOWN button
        */
 
		switch(_getch()){
		case 'q':
		case 27:
			delete [] ypos;
			textColor(0x0F);
			return -1;
		case '8':
			if(i>0){
				gotoxy(xpos,ypos[i] );
				textColor(0x0F);
				cout << m[i];
				--i;
			}
			break;
 
		case '2':
			if(i< index-1){
				gotoxy(xpos,ypos[i] );
				textColor(0x0F);
				cout << m[i];
				++i;
			}
			break;
 
			// 13 is the enter key
		case 13:
			delete [] ypos;
			textColor(0x0F);
			return i;
		}
	}
}

int printMenu(string m[], string tip[], int index){
	// My custom code, made to list index amount of menu items, instead of always 5
	int i, xpos = 10, *ypos;
	ypos = new int[index];
	for (int a = 0; a < index; a++)
		ypos[a] = a * 3;
	
    // list the menu
    for (i=0; i< index; ++i){
        gotoxy(xpos, ypos[i] );
        textColor(0x0F);
        cout << m[i];
    }
 
    // make menu available to choose
    i=0;
    while(true){
		gotoxy(xpos, ypos[i]);
		textColor(0xF0);
		// probably not the best place to put this, but first hex number indicates background color
		// next hex number indicates text color
		cout << m[i];
		gotoxy(38,2);
		cout << right << tip[i] << left;
 
        /* note : 72 -> UP button
            75 -> RIGHT button
            77 -> LEFT button
            80 -> DOWN button
        */
 
		switch(_getch()){
		case 'q':
		case 27:
			delete [] ypos;
			textColor(0x0F);
			return -1;
		case '8':
			if(i>0){
				gotoxy(xpos,ypos[i] );
				textColor(0x0F);
				cout << m[i];
				--i;
			}
			break;
 
		case '2':
			if(i< index-1){
				gotoxy(xpos,ypos[i] );
				textColor(0x0F);
				cout << m[i];
				++i;
			}
			break;
 
			// 13 is the enter key
		case 13:
			delete [] ypos;
			textColor(0x0F);
			return i;
		}
	}
}

// overloaded version of the above function, used to draw the menu
//  to a specific place. The delete command causes a heap error
int printMenu(string m[], int index, int x, int y){
	// My custom code, made to list index amount of menu items, instead of always 5
	int i, *ypos;
	ypos = new int[index];
	for (int a = 0; a < index; a++)
		ypos[a] = y + (a * 3);
	
    // list the menu
    for (i=0; i< index; i++){
        gotoxy(x, ypos[i]);
        textColor(0x0F);
        cout << m[i];
    }
    
    i=0;
    while(true){
		gotoxy(x, ypos[i]);
		textColor(0xF0);
		// probably not the best place to put this, but first hex number indicates background color
		// next hex number indicates text color
		cout << m[i];
 
        /* note : 72 -> UP button
            75 -> RIGHT button
            77 -> LEFT button
            80 -> DOWN button
        */
 
		switch(_getch()){
		case 'q':
		case 'Q':
		case 27:
			delete [] ypos;
			textColor(0x0F);
			return -1;
		case '8':
			if(i>0){
				gotoxy(x,ypos[i] );
				textColor(0x0F);
				cout << m[i];
				--i;
			}
			break;
 
		case '2':
			if(i< index-1){
				gotoxy(x,ypos[i] );
				textColor(0x0F);
				cout << m[i];
				++i;
			}
			break;
 
			// 13 is the enter key
		case 13:
			// for some reason, deleting ypos in this function causes a crash
			delete [] ypos;
			textColor(0x0F);
			return i;
		}
	}
}

void dispMenu(Player& p){
	int choice;
	Clear();
	string ar[] = {"Name", "Class", "Gender"};
	choice = printMenu(ar, 3);
	while (choice != -1){
	switch (choice){
	case 0:
		Clear();
		gotoxy(20,10);
		cout << p.getName();
		_getch();
		Clear();
		choice = printMenu(ar,3);
		break;
	case 1:
		Clear();
		gotoxy(20,10);
		cout << p.getClass();
		_getch();
		Clear();
		choice = printMenu(ar,3);
		break;
	case 2:
		Clear();
		gotoxy(20,10);
		cout << p.getGender();
		_getch();
		Clear();
		choice = printMenu(ar,3);
		break;
	}
	}
}

int printMenu(string m[], string tip[], int index, int x, int y){
	// My custom code, made to list index amount of menu items, instead of always 5
	int i, *ypos;
	ypos = new int[index];
	for (int a = 0; a < index; a++)
		ypos[a] = y + (a * 3);
	
    // list the menu
    for (i=0; i< index; i++){
        gotoxy(x, ypos[i]);
        textColor(0x0F);
        cout << m[i];
    }
    
    i=0;
    while(true){
		gotoxy(x, ypos[i]);
		textColor(0xF0);
		// probably not the best place to put this, but first hex number indicates background color
		// next hex number indicates text color
		cout << m[i];
		gotoxy(38,2);
		textColor(0x0F);
		cout << right << tip[i] << left;
 
        /* note : 72 -> UP button
            75 -> RIGHT button
            77 -> LEFT button
            80 -> DOWN button
        */
 
		switch(_getch()){
		case 'q':
		case 'Q':
		case 27:
			delete [] ypos;
			return -1;
		case '8':
			if(i>0){
				gotoxy(x,ypos[i] );
				cout << m[i];
				--i;
			}
			break;
 
		case '2':
			if(i< index-1){
				gotoxy(x,ypos[i] );
				cout << m[i];
				++i;
			}
			break;
 
			// 13 is the enter key
		case 13:
			// for some reason, deleting ypos in this function causes a crash
			delete [] ypos;
			return i;
		}
	}
}
Let me see...your grandmother's name was Beatrice?

Post Reply

Return to “Programming”