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

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

问题描述

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

在 DLL 文件 1 中:

类 abc{static bool FindSubFolders(const std::string & sFolderToCheck,std::vector

在 DLL 文件 2 中:

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

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

dbgheap.c:第 1274 行

/** 如果这个ASSERT失败,则传入了一个错误的指针.可能是* 完全是假的,或者它可能是从另一个堆分配的.* 指针必须来自本地"堆.*/_ASSERTE(_CrtIsValidHeapPointer(pUserData));

我认为这是因为内存已在 DLL 文件 1 的堆上分配,但正在 DLL 文件 2 中释放.

dbgheap.c 中的注释似乎非常坚持这是一个问题.

为什么会出现这样的问题,如果我忽略它似乎可以正常工作?有没有一种非断言失败的方法来做到这一点?

解决方案

正如 sean 已经说过的,发布版本会简单地忽略该删除语句,因此您最希望的是内存泄漏.

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

缺点是您需要将运行时系统与您的应用程序一起安装(微软为此提供了一个安装程序).它可以在您的开发机器上正常运行,因为 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天全站免登陆