在参数为map引用的dll函数中用map.insert后,在外部map析构时堆报错
直接上代码,vs2005
- C/C++ code
int _tmain(int argc, _TCHAR* argv[]){ { multimap<int,wstring> myResult; myResult.insert(pair<int,wstring>(2,_T("Ddd"))); CopyResultMap(myResult); }//myResult析构,报错点 return 0;}
dll中的CopyResultMap(myResult)函数
- C/C++ code
extern "C" _declspec(dllexport) int CopyResultMap(multimap<int,wstring>& myResult){ int i=0; myResult.insert(pair<int,wstring>(2,_T("Ddd")));//把这行代码去掉是可以的 return i;}
我的想法就在dll中插入map值,然后在main函数中用,但会报堆使用错误
有人说是dll和主函数使用的是不同的堆,所以主函数不能析构dll中insert的值,然后就内存泄露了,请问是这样不,有什么解决办法不
[解决办法]
如果只是让DLL内部调用insert的话
你可以在主程序中实现
typedef void Function(int, LPCTSTR);
void Insert(int nKey, LPCTSTR lpszString)
{
myResult.insert(pair<int,wstring>(nKey, lpszString));
}
并将DLL函数参数改为Function* pFunction的函数指针,让DLL回调主函数的Insert函数
extern "C" _declspec(dllexport) int CopyResultMap(Function* pFunction)
{
pFunction(2,_T("Ddd"));
return 0;
}
时候将