Some knowledge about why we need to new and delete everything in one module

Essence: Wrap functions to read/write things from another module.

Windows keep seperate heaps for debug and release - built DLLs and EXEs. So for example, calling a release DLL from a debug application EXE,  any memory we allocate needs to be de-allocated by us in order for the heap management to be consistent. If everything is built release, it should all come from the same heap and there should be no inconsistency.

But problem happens when VC8 DLLs and VC9 EXE work together in case of memory allocation and deallocation. It's likely that VC8 vs. VC9 DLLs and EXEs have the same issue.  I.e.,VC8-built DLL and a VC9-built application may use different heaps, even for release builds.  If so, the safest way is to deallocate and allocate memory always in one module.    

And, going further, it's very likely that different module uses different heap, which means process default heap is always the first one to consider to use, but there are so many possibility that a module use its custom heap instead of it. If failling into that case, how can another module call delete directly to free the memory assigned in one module?

We can deallocate memory allocated in another module, but it will cause heap management inconsistency.

原文地址:https://www.cnblogs.com/taoxu0903/p/1288540.html