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

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

问题描述

我有一个使用DLL文件的EXE文件,它使用另一个DLL文件。出现这种情况:

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

在DLL文件1中:

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

在DLL文件2中:

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


$ b b

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

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));

我假设这是因为内存已分配在DLL文件1的堆上, DLL文件2.

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

dbgheap.c 中的注释似乎坚持认为这是一个问题。

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?

推荐答案

正如sean已经说过,删除语句,所以最好你可以希望是一个内存泄漏。

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

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

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

缺点是您需要与应用程序一起安装运行时系统安装程序)。它将在您的开发机器上正常工作,因为Visual Studio也安装了该运行时系统,但在新安装的机器上,它将报告缺少的dll。

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 dlls.

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

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