昨天协助同事查Bug,源代码如下:
ExternalDll.h (外部DLL中)
static std::list<int> ExternalDll::getList()
{
std::list<int> iList;
iList.push_back(1);
iList.push_back(2);
return iList;
}
TestExternalDLL.cpp (exe中)
void testExternalDll()
{
std::list<int> iList = ExternalDll::getList();
} // 程序运行至此,出现“其原因可能是堆被破坏”的报错
void main()
{
testExternalDll();
}
VC 项目默认设置--》Use MFC in a static library
堆栈断点:(截图来自网络) (引用http://yaogangshi.iteye.com/blog/1001690)
后来就搜到了微软的这篇文章,dll间传递CRT对象(文件句柄、环境变量、区域信息等)存在潜在问题。
进程分配内存(显式调用new,allocate,隐式分配内存比如调用strdup,strstreambuf::str等)
然后传递给DLL进行释放。都可能因为使用了不同的CRT库造成内存非法访问和栈破坏。
每一份CRT库的都有独立和唯一一份状态,并且每一份CRT库都有自己的对管理器。DLL和EXE可能使用了不同的CRT库,所以分配和释放内存的堆不能匹配从而导致了堆破坏。
修复方法:
EXE和所有的DLL都必须动态链接/MD到同一份CRT。
参考:
http://msdn.microsoft.com/zh-cn/library/ms235460.aspx
http://blog.youkuaiyun.com/nodeathphoenix/article/details/7550546