早些时候,我遇到了一个问题,在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.
更多推荐
调试内存损坏
发布评论