Swanand
Swanand

Reputation: 4115

Programs works when /MT in settings but fails when /MD

I have a DLL and 3 Applications which uses this DLL. (These application do not run simultaneously)

Out of 3 Applications, 2 work perfectly but 1 application doesn't get response from one DLL function after some time (at 7th function call, to be specific). Also, The code works properly if I use debug version of Application OR my DLL. It stops in Release version only.

After spending 2 sleepless nights, I figured out that If I change project property of DLL from /MD to to /MT, this application works properly.

I have no clue why this thing is happening. Can anyone please explain this for the sake of a sleep deprived programmer!

Update:

I would be releasing this DLL in market and I can not say whether user application will be built is /MT or /MTD or whatever... Is there any way to make sure that it will work with any application.

Upvotes: 2

Views: 261

Answers (2)

brian beuning
brian beuning

Reputation: 2862

In Windows speak, the EXE and DLL files are modules. Each modules compiled dynamically (/MD) share one heap. So in the dynamic modules, if one module calls malloc (or new) and another module does the free (or delete) on the object all is good.

Each module compiled to link in the C runtime statically gets its own heap. If one static module allocates an object and a different static or dynamic module tries to free the object, the program will crash because the allocate and free are against different heaps.

Allocating and freeing memory across module boundaries

Upvotes: 2

papirrin
papirrin

Reputation: 2053

/MD links the runtime as dynamic, if the computer you have a runtime properly installed, then compiling as /MT would work, since the runtime will be included in the binary.

That may also explain while it works when you compile it as Debug mode, in debug mode, a debug version of the runtime is statically linked into the binary.

Look here for some discussion about the topic.

UPDATE Another problem may be that the modules that the dll were compiled with different options, as stated in this msdn article:

All modules passed to a given invocation of the linker must have been compiled with the same run-time library compiler option (/MD, /MT, /LD).

Upvotes: 0

Related Questions