“仅限 Vista” MFC 应用程序中的堆损坏
我们的应用程序是一个 MFC 应用程序,它链接到一个 win32 DLL。当应用程序调用该DLL函数时,参数“buffer”在进入函数堆栈后变成“坏指针”。这会导致应用程序崩溃。
static MyClass* Instance(string& buffer);
我将参数类型更改为“char *”,但它只会将崩溃推到函数中的下一个语句。如何检测堆损坏?
一些提示
- 即使我从应用程序一开始就调用此 DLL 函数(CWinApp 构造函数),此崩溃也是可以重现的。这种内存损坏是否是由于加载资源、清单等引起的?
- Vista 和 Win7 中会发生崩溃,但 XP 中不会发生崩溃。
- 这两个项目最近都从 Visual Studio 2002 迁移到 VS2008。
调用函数的代码
CString data = "some string";
string str = data.GetBuffer();
data.ReleaseBuffer();
MyClass *obj = MyClass::Instance(str);
Ours is a MFC application, which links to a win32 DLL. When the application invokes this DLL function, the argument "buffer" becomes a "Bad Pointer", after entering the function stack. This results in an application crash.
static MyClass* Instance(string& buffer);
I changed the argument type to "char *", but it only pushes the crash to next statement in the function. How to detect this heap corruption?
Few hints
- This crash is reproducible, even if I invoke this DLL function from the very start of our application (CWinApp constructor). Could this memory corruption be caused by loading of resources, manifest etc?
- The crash is ocurring in Vista and Win7, but not in XP.
- Both these projects were recently migrated from Visual Studio 2002 to VS2008.
Code that invokes the function
CString data = "some string";
string str = data.GetBuffer();
data.ReleaseBuffer();
MyClass *obj = MyClass::Instance(str);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两个错误:
几个自定义构建的 C++ 文件未使用 MD 开关进行编译。我们必须将 -MD 添加到自定义构建脚本中,以使 CRT 与其他对象保持一致。
LIBCMT.LIB 和 MSVCRT.LIB 之间存在 LNK2005 冲突,这些冲突由于 /FORCE 开关而被忽略。我们通过在“链接器”->“输入”
中删除 LIBCMT.LIB 解决了这些冲突。LIBCMT.LIB
感谢大家的帮助。
There were two mistakes:
Couple of custom built C++ files were not compiled with MD switch. We had to add -MD to the custom build script to make the CRT consistant with other objects.
There were LNK2005 conflicts between LIBCMT.LIB and MSVCRT.LIB, which were otherwise ignored due to the /FORCE switch. We resolved these conflicts by removing LIBCMT.LIB in Linker->Input
Thanks all for your help.
我的猜测是这是 调用约定 的错误使用或 CRT 不匹配(我使用调用约定)。
尝试构建具有相同函数签名(不执行任何操作)的存根 DLL,并使其与您的 MFC 应用程序一起使用。
这是一个 示例...
HTH
My guess is this is wrong usage of calling conventions or a CRT mismatch (i go with calling conventions).
Try building a stub DLL with the same function signature (which does nothing) and make it work with your MFC app.
Here's an example...
HTH