更改运行库选项对运行时链接的影响 [英] Effect of Changing Runtime Library Option on Runtime Linkage
问题描述
在 Microsoft Visual Studio 2015(v14.0)
我有一个解决方案包含3个项目。
其中两个项目是DLL,另一个是可执行文件。
可执行文件在运行时加载DLL并调用其函数,并交换参数;使用Window的 LoadLibrary
和 GetProcAddress
API。 运行时库
时,在发布
模式中
<的我的项目
多线程DLL
一切正常。这是多线程调试DLL
用于调试
模式。 如果我将更改为
多线程
或多线程调试
为调试
我开始获得调试声明错误或内存访问违例错误和其他类型的错误。 (当我改变它,我改变它为解决方案中的所有项目。)
我需要使用多线程
选项,以便可执行文件在目标机器上不需要C ++运行时库。如何解决这个问题?
这是因为使用 / MT
分离实体(即DLL和可执行文件)的运行时,每个都有自己的运行时,因此他们自己的堆,试图分配内存在一个和释放它在另一个将最终会错误。因为其他模块不知道分配的内存。
另一方面, / MD
所有模块共享相同的运行时,并且运行时知道的内存分配在一个,它将能够释放它在其他。因为运行时的一个实例管理整个内存。
编辑:
感谢 dxiv
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?
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.
这篇关于更改运行库选项对运行时链接的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!