Chinaunix首页 | 论坛 | 博客
  • 博客访问: 446611
  • 博文数量: 145
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1060
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-22 11:52
个人简介

专注计算机技术: Linux Android 云计算 虚拟化 网络

文章分类

全部博文(145)

文章存档

2016年(3)

2015年(21)

2014年(75)

2013年(46)

我的朋友

分类: C/C++

2015-01-27 12:00:31

heap corruption detected错误解决方法调试方法以及内存管理相关(2012-02-24 13:11:00)转载▼标签: it 分类: C/C++

1、heap corruption detected
http://vopit.blog.51cto.com/2400931/645980
 
heap corruption detected:after normal block(#xxx) at 0x xxxxxxxx
crt detected that the application wrote to menory after end of heap buffer
这是典型的内存溢出错误,常在内存的delete处发生,而且一般在debug版本中可能出现,release版本中可能并不报错.
出现这个错误的原因一般都是操作new申请的内存溢出,因为在c++中,如果用new分配一段内存,操作的时候改变了该部分的大小,在delete时就会出错.比如说如下部分:
char* p=new char[5];
strcpy(p,"aaaaa");
delete[] p;
这段代码就会出错,因为申请了一个size为5的内存,但是strcpy过去了一个size为6的字符串,因此破坏了这个指针,运行debug版本的时候就会出现先前的错误,但是在release版本中,溢出一个字节的内存很有可能是没有错误的,然后潜在的隐患是肯定存在的,因此,我们在debug遇到这样的错误时候一定要仔细检查对new出的指针的操作.
 
           ***************拷贝时,内容超出申请的空间***********
           如memcpy的时候,size参数比new出来的空间还大

 
2、heap corruption detected after normal block
   http://hi.baidu.com/linzch/blog/item/3ceb4dedef60e7d9b31cb1e5.html
     *********一般是内存溢出错误。需要检查指针对内存的申请情况*************
 
3、 heap corruption detected
     http://blog.csdn.net/fjz13/article/details/2535126
    **********sscanf(str, "X", &pCmd[i]);中pCmd分配的空间不足************
 
4、HEAP CORRUPTION DETECTED:after Normal block(#***) at 0x****.CRT detected
   that application wrote memory after end of heap buffer.
   
   错误原因:以对内在操作的过程中,所写的地址超出了,所分配内在的边界
   解决办法:在可能出错的代码处,使用_CrtCheckMemory进行检测错误的现象是这样的:

简单内存调试技术(详见网址链接查看具体调试方法)

 

一 检查内存泄漏

添加以下语句:

#define CRTDBG_MAP_ALLOC

#include

#include

比如:
int* p = new int[2];
*(p+2) = 1;
_ASSERTE( _CrtCheckMemory( ) );  //可以定位内存泄露的行

 
5、结构申请空间的时候别把
   p =(struct student *)malloc(sizeof(struct student));
   写成
   p =(struct student *)malloc(sizeof(struct student *));
 
6、关于内存堆的问题 
   http://blog.sina.com.cn/s/blog_4ae178ba010110ys.html
 
7、C语言释放链表时的问题
  
 
 current = head;
 while (current != NULL)
 {
     free(current);
     current=current->next;          
 }
 
改为
 
//释放字符子串链表空间
 current = head;
 while (current != NULL)
 {
       temp=current;
       current=current->next;
       free(temp);   
 }
 
8、内存越界的概念和调试方法
  

 

阅读(951) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~