昨天遇到一个很奇怪的问题,引用一个DLL,里面有内存开辟的操作,后来在外面释放的时候总出问题,查了查资料,后来找到了解决方案!其中引起这个问题的原因主要是跨module的内存开辟和释放可能不在同一个runtime libary,后来把DLL和EXE都设置为MTD后就好了,在这里再用别人的一篇文章(很有用),这篇文章主要说明在dll中开辟内存要和项目在同一个运行时库上面,那样就不会导致内存的泄露,而且在同一个运行时库上面让程序员更轻松的在项目中对dll中的内存操作:
Allocating and freeing memory across module boundaries
I'm sure it's been drilled into your head by now that you have to free memory with the same allocator that allocated it. LocalAlloc
matches LocalFree
, GlobalAlloc
matches GlobalFree
, new[]
matches delete[]
. But this rule goes deeper.
If you have a function that allocates and returns some data, the caller must know how to free that memory. You have a variety of ways of accomplishing this. One is to state explicitly how the memory should be freed. For example,