释放 CStringArray& 时的堆冲突来自 DLL 导出函数的参数
我开发了一个 MFC dll,其中包含具有以下原型的函数:
//DLL code
long __declspec(dllexport) GetData(CString csIdentifier, CStringArray& arrOfData)
{
//based on the identifier I must add some strings inside the string array
arrOfData.Add("...");
arrOfData.Add("...");
/*.....................*/
return 1;
}
我遇到的问题是在函数被调用之后(从可执行文件)。 arrData 的析构函数将被调用并尝试释放内存,但它不会成功,因为 arrOfData 的分配是在另一个堆上(在 dll 内部)完成的。尽管我使用相同的环境设置编译了两个应用程序(Exe 和 Dll),但在调试和发布模式下仍然存在问题。我该如何解决这个问题?
//Executable code
{
CStringArray arrData;
GetData("Identifier",arrData);
//data is accesible
}
堆冲突发生在现有代码块之前
I have developed a MFC dll containing a function having this prototype:
//DLL code
long __declspec(dllexport) GetData(CString csIdentifier, CStringArray& arrOfData)
{
//based on the identifier I must add some strings inside the string array
arrOfData.Add("...");
arrOfData.Add("...");
/*.....................*/
return 1;
}
The problem that I have is after the function gets called (from the executable). The destructor of the arrData will be called and will try to release the memory but it will not succeed because the allocation of the arrOfData was done on another heap(inside the dll). Although I have compiled both applications (Exe and Dll) using the same enviroment settings, I still have the issue in both debug and both release mode. How can I solve the issue?
//Executable code
{
CStringArray arrData;
GetData("Identifier",arrData);
//data is accesible
}
heap violation occurs just before existing the code block
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了跨 exe/dll 边界共享 MFC 对象(如 CStringArray),您需要使 DLL 成为 MFC 扩展 DLL。请参阅:https://msdn.microsoft.com/ en-us/library/h5f7ck28(v=vs.140).aspx
来自内存管理部分:
您的 DLL 函数也可能需要顶部的 AFX_MANAGE_STATE(AfxGetStaticModuleState()) 来在外部调用时设置环境属性。
In order to share MFC objects like CStringArray across an exe/dll boundary, you'll need to make the DLL be an MFC Extension DLL. See: https://msdn.microsoft.com/en-us/library/h5f7ck28(v=vs.140).aspx
From the section on Memory Management:
It's also possible that your DLL function needs AFX_MANAGE_STATE(AfxGetStaticModuleState()) at the top to property set up the environment when called externally.