0

In Microsoft Visual Studio 2015 (v14.0) I have a solution that contains 3 projects.

Two of those projects are DLLs, and the other one is the executable.
The executable loads the DLLs at runtime and calls their functions and they exchange parameters; using Window's LoadLibrary, and GetProcAddress APIs.

In Release mode, when I set Runtime Library of my projects to Multi-threaded DLL everything works fine. This is Multi-threaded Debug DLL for Debug mode.

If I change to Multi-threaded for Release or Multi-threaded Debug for Debug I start getting Debug Assertion errors or Memory Access Violation errors and other kind of errors. (When I change it, I change it for all the projects in the solution.)

I need to use Multi-threaded option so that the executable won't need C++ runtime library on the target machine. How can I solve this issue?

Ahmad Siavashi
  • 979
  • 1
  • 12
  • 29

1 Answers1

0

It was because using /MT separates runtimes of entities (i.e. DLLs and executable), with each having their own runtime, hence their own heap, trying to allocate memory in one and freeing it in the other will end up in error. Because other modules were oblivious to the memory allocated.
On the other hand, with /MD all modules share the same runtime and as the runtime is aware of the memory allocated in one, it will be able to free it in the other. Because one instance of runtime manages the whole memory.

EDIT: Thanks to dxiv comment.

Ahmad Siavashi
  • 979
  • 1
  • 12
  • 29
  • There are a few misconceptions in what you wrote. Address space is per-process, and DLLs *always* share the same address space with the process that loaded them, and with other DLLs loaded by the same process. It is perfectly safe to *access* memory allocated in one DLL function from anywhere else within the same process. What does *not* work with `/MT` is allocating memory (`new`) in code in one DLL, then freeing it (`delete`) in code from another DLL, because with `/MT` each DLL has its own copy of the CRT statically linked. See for example [this answer](http://stackoverflow.com/a/8157934). – dxiv Aug 22 '16 at 00:38
  • 1
    @dxiv You are right, thank you for the comment. I modified the answer. – Ahmad Siavashi Aug 22 '16 at 04:06