最近写GIF解析,使用了很多结构体指针。
常常出现在指针delete的时候 异常。
具体如下:
RGBQUAD *GIFGlobolColorTable;//保存全局颜色列表GIFGlobolColorTable = new RGBQUAD[gct]; memset(GIFGlobolColorTable,0,gct*sizeof(RGBQUAD));if (GIFGlobolColorTable != NULL){GifColorType * pColor = GifIn->SColorMap->Colors;for (int i = 0;irgbBlue = (BYTE)pColor->Blue;GIFGlobolColorTable->rgbGreen = (BYTE)pColor->Green;GIFGlobolColorTable->rgbRed = (BYTE)pColor->Red;GIFGlobolColorTable->rgbReserved = 0;pColor++;GIFGlobolColorTable++;}}else {exit(EXIT_FAILURE);}mycolortable = GIFGlobolColorTable - gct;
最后 delete GIFGlobolColorTable 的时候异常。
调试发现 在循环中GIFGlobolColorTable 指针一直在做++ ,导致最后delete的时候,指针的位置已经偏移申请的位置gct这么长
改正方法如下:只需改变最后两句
GIFGlobolColorTable-=gct; mycolortable =GIFGlobolColorTable;