I just spent a week polishing out a planet *. prj type one week project, and I feel a lot of GAINS. I would like to share with you.
This project is designed to solve the heap upload uption problem, but we know that normally, when we allocate a piece of memory in the heap, even though there may be out-of-bounds operations during use, however, the system often does not directly report an error when crossing the border. Instead, it throws a heap commit uption during the last delete operation. This is because the heap allocation granularity of the operating system is 4 kb. If the memory out of bounds is not just on that 4 K boundary, it will not cause the operating system to be alert and report errors.
If we only check the call stack when the heap resume uption occurs, we will find little useful information. Manual check is also performed in a large piece of code
Not very realistic.
In this case, the heap debugging tool-pageheap can help a lot. This tool forces the program to allocate each heap to the end of a 4 K memory page and mark the next 4 K
The adjacent memory is unavailable, and an error is reported immediately when it is out of bounds. However, when I first wrote the following code for testing, I found that the Code still reports an error until the delete operation:
Char * pctest = new char [10];
Pctest [10] = 1;
//...
//...
//...
Delete [] pctest;
When the new operation is performed, the actual memory in debug mode is
The situation is as follows:
0x0342aff0 CD
0x0342aff8 cd fd D0 D0
Next 4 K :?? ?? ?? ?? ?? ?? ?? ??
10 CDs are available memory areas, and 4 FD are used for debugging in debug mode. Two D0 play a placeholder role. Because the first address of memory allocation must be a multiple of 8, although pageheap tries its best to allocate the newly allocated memory to the end of 4 K, but it was written on the FD and D0 at the beginning, and did not go beyond the 4 K world.
If pctest [16] = 1, the system considers the operation as illegal. In conclusion, even if pageheap is used, the error points cannot be located immediately in some cases. However, in most cases, pageheap is useful.
Appendix:
The several FD are used for debugging in debug mode. As mentioned above, operating system-level troubleshooting can only detect overflow in 4 K world, but in debug mode, if the four FD are found to have been modified during the delete operation, an error is returned. That is to say, the following code returns an error when the four FD are changed during the delete operation:
Char * pctest = new char [1];
Pctest [4] = 1;
Delete [] pctest;
However, the following changes to D0, but everything is normal:
Char * pctest = new char [1];
Pctest [5] = 1;
Delete [] pctest;
The emergence of D0 should be due to the problem of byte alignment. In my environment, it seems that all types of D0 are 8-byte alignment, bytes...
In the release mode, both FD and D0 will not appear, and there will be no overflow check. Therefore, the following code will report an error in debug mode, while release is normal:
Char * pctest = new char [1];
Pctest [4] = 1;
Delete [] pctest;