从ACE文档中看到如下的内容,总结就是: 从同一个堆上分配的内存一定要释放到同一个堆中,否则系统有可能因此崩溃。
这一点在使用基于动态链接库的应用中尤为重要,因为可以出现在主程序中分配的资源,在动态库中使用,然后内存在动态库中被释放。
A surprisingly common misconception is that simply ensuring the proper matching of calls to operator new() and operator delete() (or calls to malloc() and free()) is sufficient for correct heap management. This strategy relies on the implicit assumption that there’s one universal heap per process. In practice, however, a heap is simply a memory area managed by some run-time component, such as the C or C++ run-time library. If an executing program is exposed to multiple run-time library instances, it’s likely there will be multiple heaps as well.
For example, Windows supplies multiple variants of the C/C++ run-time library, such as Debug versus Release and Multithreaded versus Single-threaded. Each of these variants maintains its own heap. Memory allocated from one heap must be released back to the same heap. Thus, correct heap management requires not only matching the proper method/function calls, but also making them through the same run-time library. It’s easy to violate these requirements when code from one subsystem or provider frees memory allocated by another.

本站原创文章,转载请注明出处。