2

From Does msvcrt uses a different heap for allocations since (vs2012/2010/2013), i learned it is possible that delete obj across module boundaries even use MT option from ms vc2012 or newer,amazingly.

But,after i did some test i am confused(My platform is vc2013 update4 in win7). I new an obj in dll, and delete it in exe. When, dll and exe both compiled with MT option,it really looks like runing well. But, when dll and exe both compiled with MTd there is an assert error '_pfirstblock == phead'.

Can someone tell me that using module which is compiled by MT/MTd option can free memory across module boundaries in vs2013?

Community
  • 1
  • 1
linus linus
  • 161
  • 4

1 Answers1

2

Yes, the CRT allocates from the default process heap since VS2012. The one returned by GetProcessHeap(). Primary motivation for that change was probably not the improved module interop, VS2012 initially shipped without support for XP so there was no longer any need to create a private heap and call HeapSetInformation() to turn on the low-fragmentation heap. Vista and up have it enabled by default.

That didn't last, a storm of protests forced them to put XP support back. Without otherwise changing the code, I have never seen anybody complain about that. Makes you wonder if perceived need to support an ancient OS does in fact match actual need.

It doesn't otherwise do anything to solve the other problems caused by having multiple copies of the CRT in a process. Like /MT vs /MTd, your case, the debug build of the CRT adds extra metadata to an allocated memory block to detect heap corruption and bad pointer usage. That is missing from a block allocated by the non-debug CRT. So kaboom when you pass that pointer to a free() or ::operator delete call in the debug CRT, it can't find the metadata back.

And the range of other issues caused by global state in the CRT. Like errno, setlocale(), strtok(), strerror(), asctime(), gmtime(), etcetera.

So basic advice does not change, only use /MT for simple single module programs (just an EXE, no DLLs) or DLLs with a very carefully designed interface.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I appreciate your answers, thank you very much.i am curious and want to know is there any negative consequence about releaseing memory across modules in vs12 or other advanced version?Although Microsoft doesn't intend to do this. – linus linus May 21 '15 at 03:01