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;
}
}
}
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;
}
}
}
Advertising

