广州网站站建设培训,如何开发医院,帝国cms企业网站,一般通过男网友最近写的模块#xff0c;在独立的应用程序中测试是没问题的#xff0c;但把它装配成DLL后#xff0c;再在另一个应用程序中调用时却出现了内存错误。程序的模块链接关系大概是这样的#xff1a; module就是我所写的模块#xff0c;在这里被封装为DLL#xff0c;因为要使用… 最近写的模块在独立的应用程序中测试是没问题的但把它装配成DLL后再在另一个应用程序中调用时却出现了内存错误。程序的模块链接关系大概是这样的 module就是我所写的模块在这里被封装为DLL因为要使用json相关的功能该DLL链接了一个静态库 (jsoncpp.lib)。最后在应用程序中导入并使用module.dll同时因为在应用程序中也需要用到json所以应用程序也链接了jsoncpp.lib。 以下用一些伪代码来描述这些模块间的调用关系以具现出这个错误。 jsoncpp.lib为c提供了功能齐全的json操作其核心的类是Json::Value。阅读本篇文章你无需了解太多json module.dll中导出了一个接口
//ModuleClass.h
#include json/value.h#if defined MODULE_EXPORTS
#define MODULE_EXPORTS __declspec(dllexport)
#else
#define MODULE_EXPORTS __declspec(dllimport)
#endifclass ModuleClass
{
public:MODULE_EXPORTS void AllocSomeMemory( Json::Value root ){// 这将申请一些内存因为会new出一个Json::Value并append到root上root.append( testString );}
};
应用程序1#include json/value.h2#include ModuleClass.h3int main()4{5 Json::Value root;6 ModuleClass::AllocSomeMemory( root );7}在Debug模式下当main函数执行完毕对Json::Value root进行析构时程序便出现了异常。分析下很显然调用ModuleClass::MallocMemoryHere时申请的内存是在module.dll中申请的而对这些内存的析构则是在应用程序.exe中进行的析构root会同时析构append在root上的所有子Json::Value。不过这是异常的真正原因么追踪到异常的出错点dbgheap.c文件中那句ASSERT语句。1/*2* If this ASSERT fails, a bad pointer has been passed in. It may be3* totally bogus, or it may have been allocated from another heap.4* The pointer MUST come from the local heap.5*/6_ASSERTE(_CrtIsValidHeapPointer(pUserData));注释中的最后一句话”The pointer MUST come from the ‘local’ heap“引起了我的警惕难道对于内存的申请和释放不是在同一个heap上除了‘local’ heap还有一个什么heap么。去MSDN上搜索了关于_CrtIsValidHeapPointer似乎找到了答案以下这段话是MSDN上对于_CrtIsValidHeapPointer的介绍The _CrtIsValidHeapPointer function is used to ensure that a specific memory address is within the local heap. The local heap refers to the heap created and managed by a particular instance of the C run-time library. If a dynamic-link library (DLL) contains a static link to the run-time library, it has its own instance of the run-time heap, and therefore its own heap, independent of the application’s local heap. When _DEBUG is not defined, calls to _CrtIsValidHeapPointer are removed during preprocessing.注意字体加粗的部分这不正应对我的情形么错误不在于DLL中申请的内存在EXE中释放而在于如果这个DLL拥有一个静态链接它就会拥有独立的运行时堆独立于应用程序的堆。这样对于内存申请和释放并不是在同一个堆上进行的当然出错了。解决虽然MSDN上最后说如果把项目改成release的这个ASSERT就将避免但这是放纵内存泄露最好的解决办法是将静态链接也改成动态链接这样就使得DLL能够和应用程序共享同一个堆错误也得以避免。于是我修改了jsoncpp的项目配置生成jsoncpp的动态链接库而不是使用静态库重新导入到module.dll中错误解决。