调试内存损坏

编程入门 行业动态 更新时间:2024-10-08 20:32:40
本文介绍了调试内存损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

早些时候,我遇到了一个问题,在C动态内存(视觉工作室)。我有一个或多或少的工作程序释放缓冲区之一时扔了运行时错误。这是一个明确的内存损坏,程序写了缓冲区的末尾。

我的问题是,这是非常费时追查。该错误是在损坏后一路下跌扔了,我不得不手动调试整个运行发现时的缓冲区结束改写。

是否有任何工具\\方式来协助追查这个问题?如果该程序会马上崩溃我早就发现了这个问题快了很多......

问题的例子:

为int * PNUM =的malloc(10 * sizeof的(INT));// ||// \\ /的for(int i = 0; I< 13;我++){PNUM [I] = 3;}//错误....免费(PNUM);

解决方案

我使用页堆。这是Microsoft提供的工具,它改变了分配是如何工作的。随着页堆,当你调用malloc,分配被四舍五入到最接近的页面(内存块),并设置为不读虚拟内存的一个附加页面/不写放置后。你分配的动态内存对齐,使您的缓冲区末尾只是页面的虚拟页面之前结束之前。这样,如果你去你的缓冲区的边缘,往往用一个字节,调试器可以很容易地抓住它。

Earlier I encountered a problem with dynamic memory in C (visual studio) . I had a more or less working program that threw a run-time error when freeing one of the buffers. It was a clear memory corruption, the program wrote over the end of the buffer.

My problem is, that it was very time consuming to track down. The error was thrown way down after the corruption, and i had to manually debug the entire run to find when is the buffer end overwritten.

Is there any tool\ way to assist in tracking down this issue? if the program would have crashed immediately i would have found the problem a lot faster...

an example of the issue:

int *pNum = malloc(10 * sizeof(int)); // || // \/ for(int i = 0; i < 13; i++) { pNum[i] = 3; } // error.... free(pNum);

解决方案

I use pageheap. This is a tool from Microsoft that changes how the allocator works. With pageheap on, when you call malloc, the allocation is rounded up to the nearest page(a block of memory), and an additional page of virtual memory that is set to no-read/no-write is placed after it. The dynamic memory you allocate is aligned so that the end of your buffer is just before the end of the page before the virtual page. This way, if you go over the edge of your buffer, often by a single byte, the debugger can catch it easily.

更多推荐

调试内存损坏

本文发布于:2023-11-23 22:12:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!