如何正确编写 C++ DLL 也将在调试模式下加载而不会崩溃
我的问题涉及本机 C++ DLL(Visual Studio 2005,如果重要的话)以及如何编写它们以确保:
- 当 DLL 在发布模式下编译时,它将由在发布或调试模式下编译的 EXE 正确加载(第一优先)
- 当DLL在调试模式下编译时,它也将被在调试模式下编译的EXE正确加载。
今天我有一个 C++ 本机 DLL,可以在 DLL 释放/EXE 释放模式下加载并正常工作。 DLL 加载但在 DLL 释放/EXE 释放模式下无法正常工作(函数调用返回意外结果)(这是一个大问题,因为它阻止我调试 EXE,这是我的主要目标)并崩溃DLL 调试/EXE 调试模式下的堆损坏。
我知道存在一个与 CRT 相关的问题,需要在 DLL 和 EXE 之间进行 CRT 隔离。通常,这个问题可以通过在 DLL 中将运算符 new/new[]/delete/delete[] 设为私有并通过允许 EXE 动态对象创建的 create()/release() 函数包装它们来解决。
我的问题是:在我开始朝这个方向重构所有代码之前,我还需要做些什么才能避免此类问题? CRT 隔离可能会修复我的 DLL 调试/EXE 调试崩溃,但我不确定它是否会修复 DLL 释放/EXE 调试问题。
有什么提示吗?有人已经遇到过这个问题吗?
谢谢, 阿尔。
my problems deals with native C++ DLLs (Visual Studio 2005, if it matters) and how to write them in order to insure that:
- when the DLL is compiled in release mode, it will be correctly loaded by an EXE compiled in release or debug mode (first priority)
- when the DLL is compiled in debug mode, it will be correctly loaded by an EXE compiled in debug mode too.
Today I have a C++ native DLL that loads and works fine in DLL-release/EXE-release mode. The DLL loads but doesn't work fine (function calls return unexpected results) in DLL-release/EXE-release mode (and this is a huge problem, because it prevents me from debugging the EXE, which is my main goal) and crashes on a heap corruption in DLL-debug/EXE-debug mode.
I know that there is a CRT-related problem that requires CRT-isolation between DLL and EXE. Normally this problem is solved by making operators new/new[]/delete/delete[] private in the DLL and wrapping them by create()/release() functions that allows to the EXE dynamic object creation.
My question is: before I start re-factoring all my code in that direction, is there something else I need to do in order to avoid these kind of problems ? CRT-isolation will probably fix my DLL-debug/EXE-debug crash, but I'm not sure it will fix the DLL-release/EXE-debug problem.
Any hint ? Anyone having already been confronted to this problem ?
Thanks,
Al.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在公共 Dll h 文件中使用特定于配置的代码。例如:
当Dll在调试配置下编译时,sizeof(ExportedClass)包含额外的4个字节。当客户端代码编译该文件为Release配置时,sizeof(ExportedClass)会少4个字节。结果未定义。
另外,不要使用任何具有特定于配置大小的类型作为 Dll 公共接口的一部分:返回值和参数。
Don't use configuration-specific code in public Dll h-files. For example:
When Dll is compiled in Debug configuration, sizeof(ExportedClass) contains additional 4 bytes. When this file is compiled by client code is Release configuration, sizeof(ExportedClass) is 4 bytes less. Result is undefined.
Also, don't use any types which have configuration-specific size, as part of Dll public interface: return values and parameters.
我首先要解决调试/调试场景。堆损坏表明存在严重的问题,在未解决之前,您无法保证任何其他方案都能正常工作。事实上,我认为任何其他场景都无法正常工作。
另外,我不知道为什么你会认为堆损坏需要通过 CRT 隔离来解决,而不是 dll 或 exe 代码中的问题。 dll 和 exe 是否链接到不同版本的 CRT?
I would first and foremost solve the debug/debug scenario. The heap corruption is an indicator of a serious existing problem, and until not solved, you have no guarantee that any other scenario will work properly. in fact, I would argue that any other scenarios will not work properly either.
Also, I am not sure why would you think that the heap corruption needs to be solved by CRT isolation and is not a problem in the dll or exe code. Are the dll and the exe linked against different versions of the CRT?