释放在不同的DLL中分配的内存 [英] Freeing memory allocated in a different DLL

查看:177
本文介绍了释放在不同的DLL中分配的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用DLL文件的EXE文件,该文件正在使用另一个DLL文件。这种情况已经出现:



在DLL文件1中:

  class abc 
{
static bool FindSubFolders(const std :: string& sFolderToCheck,
std :: vector< std :: string>& vecSubFoldersFound);
}

在DLL文件2中:

  void aFunction()
{
std :: vector< std :: string>文件夹;
std :: string sLocation;
...
abc :: FindSubFolders(sLocation,folders)
}

在发行模式下,一切正常。但是在调试模式下,我在文件夹向量中的 std :: strings 之一的析构函数中提出断言失败(文件夹在结束时超出范围) of aFunction):



dbgheap.c:line 1274

  / * 
*如果此ASSERT失败,则传入错误的指针。它可能是
*完全虚假,或者可能已从另一堆。
*指针必须来自本地堆。
* /
_ASSERTE(_CrtIsValidHeapPointer(pUserData));

我假设这是因为内存已经分配给DLL文件1的堆,但正在被释放DLL文件2。



dbgheap.c 中的注释似乎很坚定,这是一个问题。



为什么这样的问题呢,如果我忽略它似乎工作正常吗?是否有一种不断言的失败方式?

解决方案

正如sean已经说过的那样,发布版本简直忽略了删除语句,所以最好的希望是内存泄漏。



如果您可以控制如何编译两个DLL文件,请确保使用多线程调试运行时库的DLL(/ MDd)或多线程DLL(/ MD)设置。这样,两个DLL文件将使用相同的运行时系统并共享相同的堆。



缺点是您需要将安装运行时系统与应用程序(Microsoft提供一个安装程序)。 Visual Studio安装该运行时系统后,在开发机器上可以正常运行,但是在新安装的机器上,它会报告缺少的DLL文件。


I have an EXE file using a DLL file which is using another DLL file. This situation has arisen:

In DLL file 1:

class abc
{
    static bool FindSubFolders(const std::string & sFolderToCheck, 
                               std::vector< std::string > & vecSubFoldersFound);
}

In DLL file 2:

void aFunction()
{
    std::vector<std::string> folders;
    std::string sLocation;
    ...
    abc::FindSubFolders(sLocation, folders)
}

In release mode, everything works fine. But in debug mode, I come up with an assertion failure in the destructor of one of the std::strings in the folders vector (when folders goes out of scope at the end of aFunction):

dbgheap.c : line 1274

/*
 * If this ASSERT fails, a bad pointer has been passed in. It may be
 * totally bogus, or it may have been allocated from another heap.
 * The pointer MUST come from the 'local' heap.
 */
_ASSERTE(_CrtIsValidHeapPointer(pUserData));

I assume this is because the memory has been allocated on DLL file 1's heap, but is being freed in DLL file 2.

The comment in dbgheap.c seems pretty insistent that this is a problem.

Why is this such a problem, when it seems to work fine if I just ignore it? Is there a non-assertion-failing way of doing this?

解决方案

As sean has already said, the release build simply ignores that delete statement, so the best you can hope for is a memory leak.

If you have control over how both DLL files are compiled, make sure to use the Multi-threaded Debug DLL (/MDd) or Multi-threaded DLL (/MD) settings for the runtime library. That way, both DLL files will use the same runtime system and share the same heap.

The downside is that you need to install the runtime system together with your application (Microsoft offers an installer for that). It will work fine on your development machine since Visual Studio installs that runtime system too, but on a freshly installed machine it will report missing DLL files.

这篇关于释放在不同的DLL中分配的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆