Debug Error: HEAP CORRUPTION DELETE

来源:互联网 发布:数据分析职业发展 编辑:程序博客网 时间:2024/04/29 20:30


今天debug程序,delete的时候出现了HEAP CORRUPTION DELETE 的错误。如下所示:



代码大体流程如下:

int *p = NULL;

p = new int [n]; // n 已知

if(p == NULL)

{  

  return -1;

}

function (p,n); //该函数会对p指向的值进行赋值

 if(p != NULL)

{

 delete [] p;

 p = NULL;

}


在语句“delete [] p;”时出错。

       

百度了一下这个问题,都说HEAP CORRUPTION DELETE 是内存泄露的问题。多发生在delete时。于是各种检测代码,发现应该是申请了足够多的内存,n 足够大,在function()里面,对指针p没有索引大于 n-1的操作。在function()调用前后指针的首地址也没有发生变化。

同时,也发现其他人出现这个问题,大多显示的是:

heap corruption detected: after normal block(#xxx) at 0x xxxxxxxx
crt detected that the application wrote to menoryafter end of heap buffer;

     而我出现的问题则是:

heap corruption detected: before normal block(#xxx) at 0x xxxxxxxx
crt detected that the application wrote to menory before start of heap buffer;

     是不是问题出现在这段内存的前面呢?经过调试发现,中间调用的函数function()里出现了类似p[-1] = ?这样的操作。于是,成功修改通过。

0 0