Tips for solving heap corruption
Problem Description
Prior to the implementation of the watermark extraction process, encountered a strange anomaly. To better illustrate the problem, assume that I am running the function f () with the following code:
f(){code A;//该段代码在读取一个文件中的记录 for(int03; i ++) { code B; }}
The program is interrupted at run time, and the interrupt message is: Windows has triggered a breakpoint in ApplicationGUI.exe. This can be caused by a heap corruption, which indicates that there is a bug in ApplicationGUI.exe or any DLL it loads.
Click Continue in the Break message box and the following message appears:
As can be seen from the interrupt message, the reason for the program interruption is that the heap is corrupted. Baidu a piece of 2 error message, probably understand is memory allocation out of the problem.
Solution Solutions
This starts with breakpoint debugging, where the program breaks are not deterministic, sometimes interrupted when running code a, sometimes interrupted when code B is run for the first time, and sometimes it is interrupted when the F function is almost out of operation. It took me quite a long time to debug the program.
However, as stated above, using breakpoint debugging simply cannot locate the code statement that actually has the problem. Therefore, I used the "annotation method" that I used before. The practice is to comment out all the code after the for statement, in the F function only keep code a code, if the program is interrupted, start at the end of code A, from the bottom of the code A to gradually comment out the codes A, if the program is not interrupted, indicating that the location of the interrupt is not in code a, So start from the end of code A and gradually remove the comment from the code below it.
In my question, when I commented out the code after the For statement, the program was interrupted, explaining that the code that caused the interruption was within code A, narrowing it down in turn, and finally found the source of the problem. The original is to read the file, the declared space is smaller than the contents of the file, so there is a pointer out of bounds.
Summary
This memory allocation and release caused by errors is easier to find than logic errors, but this error is often not well positioned to the specific location, so when set to allocate memory space, for the pointer to request memory, it must be careful to avoid the above error.
Tips for solving heap corruption