zoukankan      html  css  js  c++  java
  • heap corruption detected错误解决方法调试方法以及内存管理相关

    1、heap corruption detected
     
    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
         *********一般是内存溢出错误。需要检查指针对内存的申请情况*************
     
    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.
        http://bbs.ednchina.com/BLOG_ARTICLE_2103923.HTM
       错误原因:以对内在操作的过程中,所写的地址超出了,所分配内在的边界
       解决办法:在可能出错的代码处,使用_CrtCheckMemory进行检测
    错误的现象是这样的:

     

     

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

     

     

    一 检查内存泄漏

    添加以下语句:

     

    #define CRTDBG_MAP_ALLOC

    #include <stdlib.h>

    #include <crtdbg.h>

    比如:
    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、关于内存堆的问题  
     
    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、内存越界的概念和调试方法

    http://blog.sina.com.cn/s/blog_622bd16601018zzu.html

    http://www.cnblogs.com/lzjsky/archive/2010/09/27/1836807.html

    http://bbs.chinaunix.net/thread-1008031-1-1.html

  • 相关阅读:
    week4
    week3
    2017福州大学面向对象程序设计寒假作业二
    Week2
    10个android开发必备的开源项目
    Day Ten
    Day Nine
    Day Eight
    Day Seven
    Day Six
  • 原文地址:https://www.cnblogs.com/pengkunfan/p/3828874.html
Copyright © 2011-2022 走看看