谈"DAMAGE:after Normal block"的解决方法

简介: 在释放内存时,崩溃了,出现了如下错误: User breakpoint called from code at 0x7c921230 Debug Assertion Failed! Program:.

在释放内存时,崩溃了,出现了如下错误:

User breakpoint called from code at 0x7c921230

Debug Assertion Failed! Program:...

File: dbgheap.c

Line: 1011 Expression: _CrtIsValidHeapPointer(出现问题的指针)

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

如果点击忽略,则继续弹出对话框 Debug Error! Program:...

DAMAGE: after Normal block (#4826967) at 0x2000E148.

(Press Retry to debug the application)

正如上次所述(http://hi.baidu.com/ablenavy/blog/item/6092524e88ff020db2de0516.html

(DAMAGE:after Normal block的解决方法)

这次的原因依然是内存申请得太小!

具体代码如下:

#define P2Node_LEN_1 (1024 * 10)

#define P2Node_LEN_2 1024 // Sct_Node是一个结构体

Sct_Node **p2Node; //定义一个指向Sct_Node的指针的指针,相当于二维数组。

// 空间申请

p2Node = (Sct_Node **)malloc(P2Node_LEN_1 * sizeof(Sct_Node *));

for (int i = 0; i < P2Node_LEN_1; i++)

{

    p2Node[i] = (Sct_Node *)malloc(P2Node_LEN_2 * sizeof(Sct_Node));

}

// 向p2Node插入数据

pSharedData->p2Node[i][j] = sct_Node; // 释放空间

for ( int i = 0; i < P2Node_LEN_1; i++ )

{

    free( p2Node[i] ); //经调试,在该语句中出现崩溃。

}

free( p2Node );

经跟踪程序发现,在向p2Node插入数据时,j的值超过了1024,但可以正常插入,不会出现错误,等释放空间时才出现错误。

解决办法:

将 #define P2Node_LEN_2 1024 改为:

#define P2Node_LEN_2 (1024 * 10)

如何从根本上消除这种错误?!

其实很简单,在插入时加入边界检查,如下:

将插入语句:

pSharedData->p2Node[i][j] = sct_Node;

改为:

if ( j >= P2Node_LEN_2 )

{

    cout << "Error! Out of memory! P2Node_LEN_2 is too small" << endl;

    exit(1);

}

pSharedData->p2Node[i][j] = sct_Node;

这样就可以避免出现上述的崩溃现象了。

目录
相关文章
|
4月前
|
数据安全/隐私保护 Docker 容器
error: Could not get shadow information for NOUSER 问题如何处理
【6月更文挑战第15天】error: Could not get shadow information for NOUSER 问题如何处理
480 3
|
5月前
|
数据安全/隐私保护
error: Could not get shadow information for NOUSER问题如何处理
【5月更文挑战第15天】error: Could not get shadow information for NOUSER问题如何处理
102 3
|
10月前
display:block小技巧
display:block小技巧
48 0
排错-LR安装No Background bmp defined in ...的解决办法
排错-LR安装No Background bmp defined in ...的解决办法
112 0
display:none到display:block失效问题及解决办法
display:none到display:block失效问题及解决办法
328 9
|
流计算
四十四、nimbus,supervisor进程自动停止(Read a frame size of ..., which is bigger than the maximum allowable...)
四十四、nimbus,supervisor进程自动停止(Read a frame size of ..., which is bigger than the maximum allowable...)
四十四、nimbus,supervisor进程自动停止(Read a frame size of ..., which is bigger than the maximum allowable...)
|
算法 开发者
关于 加载图片"Corrupt JPEG data: premature end of data segment" 的解决方法
关于 加载图片"Corrupt JPEG data: premature end of data segment" 的解决方法
关于 加载图片"Corrupt JPEG data: premature end of data segment" 的解决方法
|
Python
如何解决matplotlib运行出现的Invalid DISPLAY variable
最近在服务器上运行matplotlib相关的脚本时遇到了"Invalid DISPLAY variable"报错,从报错中就可以知道这是因为没有显示设备导致的报错。
1673 0