I’m handling a project based on cocos2dx on Android.And the game always crashes because my code causes the heap corruption.But I search the code too long time to find the error code?Besides the code is not written by me.Anyone have any ideas to debug the heap corruption problem ? Just give me some tips. Thanks!
I solve it. It’s a stupid error. My workmate use such code cause overrunning storage:
StaticItems *si;
char* szTexName = new char;
char* szTexPos = new char;
int szItemType = 0;
float scale_x = 1;
float pos_x = 0.0;
float pos_y = 0.0;
float rotate = 0.0;
int shapeindex = 0;
char* szName = new char;
for (int i = 0;i<8;i++)
{
//if (atts[i*2] == "Tex")
if (strcmp(atts[i*2],"Tex") == 0)
{
sscanf(atts[i*2+1],"%[^|]|%[^|]",szTexName,szTexPos);
}
//if (atts[i*2] == "class")
if (strcmp(atts[i*2],"class") == 0)
{
sscanf(atts[i*2+1],"%d",&szItemType);
}
//if (atts[i*2] == "Scale")
if (strcmp(atts[i*2],"Scale") == 0)
{
sscanf(atts[i*2+1],"%f",&scale_x);
}
//if (atts[i*2] == "Pos")
if (strcmp(atts[i*2],"Pos") == 0)
{
sscanf(atts[i*2+1],"%f,%f",&pos_x,&pos_y);
}
//if (atts[i*2] == "Rot")
if (strcmp(atts[i*2],"Rot") == 0)
{
sscanf(atts[i*2+1],"%f",&rotate);
}
//if (atts[i*2] == "Name")
if (strcmp(atts[i*2],"Name") == 0)
{
sscanf(atts[i*2+1],"%s",szName);
}
//if (atts[i*2] == "ShapeIndex")
if (strcmp(atts[i*2],"ShapeIndex") == 0)
{
sscanf(atts[i*2+1],"%d",&shapeindex);
}
szTexName szTexPos is not large enough to hold the str returned by sscanf,etc.sscanf return string “a1.txt” can overrun szTexName’s heap storage.