内存分配和跨区域分配 [英] Memory allocation and deallocation across dll boundaries

查看:181
本文介绍了内存分配和跨区域分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白,一个DLL中的内存分配随后随后在另一个中释放可能会导致所有类型的问题,特别是关于CRT。当出口STL容器时,这些问题尤其成问题。我们之前曾经遇到过这些问题(在编写与我们的图书馆链接的自定义Adobe插件时),我们通过定义我们在我们所有容器中使用的分配器来解决这些问题,例如:

  typedef std :: vector< SessionFields,
OurAllocator< SessionFields> >
VectorSessionFields;

typedef std :: set< SessionFields,
std :: less< SessionFields>
OurAllocator< SessionFields> >
SetSessionFields;

当我们将代码传递到/从我们的代码传递类型时,这一切都很好,但是我们遇到了一个问题我们现在不得不在Adobe的SDK中调用一个函数,返回一个填充的向量,当它超出范围时会导致崩溃。



显然,这是一个问题当我的代码中最后被释放时,内存被分配到属于不同堆的Adobe的SDK中。所以我想,也许我可以做一些聪明的东西,以某种方式覆盖或导出SDK中使用的分配器,所以我可以用它来清理从函数返回的容器。



我也在写一个包装器或某种thunking层,这样STL容器就可以在我的代码和SDK之间安全地组合起来(尽管这听起来很杂乱,但是这个)。



或者,我也在使用 GetProcessHeaps 来识别SDK内使用的堆,并尝试免除此有没有人有任何关于我们如何解决这个问题的建议?



讽刺的是,Adobe Source Libraries有一个 adobe :: capture_allocator 专门用这种D编写的类LL安全考虑。它的工作原理是捕获本地的新的 delete 在这一点上它被实例化,并携带它们围绕着物体的一生。 (见 adobe :: new_delete_t 有关如何执行此操作的详细信息,特别是实施此处)。取消分配将与捕获删除例程,保证无论您正在使用正确的删除删除什么。



您可以在 capture_allocator =nofollow noreferrer> version_1 在Adobe Source Libraries中输入,如 adobe :: any_regular_t adobe :: c opy_on_write capture_allocator 应该与所有STL容器类型兼容。



更新: capture_allocator 不符合标准,因为它保留状态。这不应该对其可用性造成很大的阻碍,但它的确意味着它的使用不能保证符合标准容器。


I understand that memory allocations made in one dll then subsequently free'd in another can cause all sort of problems, especially regarding the CRT. These sorts of problems are especially problematic when it comes to exporting STL containers. We've experienced these sorts of problems before (when writing custom Adobe plugins that linked with our libraries) and we've worked round these issues by defining our own allocator that we use in all our containers, eg:

typedef std::vector < SessionFields, 
        OurAllocator < SessionFields > > 
        VectorSessionFields;

typedef std::set < SessionFields, 
        std::less < SessionFields >, 
        OurAllocator < SessionFields > > 
        SetSessionFields;

This has worked well when passing types to/from our code, however we've hit a problem in that we're now having to call a function in Adobe's SDK that returns a populated vector which causes a crash when it goes out of scope.

Obviously, it's a problem with memory being allocated in Adobe's SDK belonging to a different heap when it's finally free'd in my code. So I'm thinking that maybe I could do something clever like somehow overriding or exporting the allocator used in their SDK so I could use it to clean up containers returned from their functions.

I'm also looking at writing a wrapper or some sort of thunking layer whereby STL containers would be safely marshalled between my code and the SDK (although this does sound very messy).

Alternatively, I'm also looking at using GetProcessHeaps to identify the heap used from within the SDK, and try to free against this heap, instead of the default heap.

Has anyone any advice on how we can solve this problem?

解决方案

Ironically enough, the Adobe Source Libraries has a adobe::capture_allocator class that was written specifically with this kind of DLL safety in mind. The way it works is to capture the local new and delete at this point it is instantiated, and to carry them both around for the lifetime of the object. (See adobe::new_delete_t for details on how it does this, especially the implementation here.) Deallocations take place with the captured delete routine, guaranteeing that no matter where you are you are deleting with the proper delete.

You can see capture_allocator used throughout the version_1 types in the Adobe Source Libraries, such as adobe::any_regular_t and adobe::copy_on_write. capture_allocator should be compatible with all STL container types as well.

Update: capture_allocator is not standard-compliant because it retains state. This should not be a big hindrance to its usability, but it does mean its use is not guaranteed to work with standard-compliant containers.

这篇关于内存分配和跨区域分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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