Memory allocation and deallocation across dll boun

2020-01-29 01:53发布

问题:

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?

回答1:

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.



回答2:

At the moment we're working on a dll which exposes C++ functionality via a C interface (for the sake of C# being capable of using the said dll).

for instance : the dll has a struct myStruct_s the interface exposes the following functions :

interface.h

#ifndef INTERFACE_TYPES_H
# error Please include interace_types.h
#endif
    myStruct_s * CreateTheStruct() { return new myStruct_s(); }
    void DestroyTheStruct(myStruct_s * the_struct) { delete the_struct; }
    void DoSomethingToTheStruct(myStruct_s * the_struct);

interface_types.h

#define INTERFACE_TYPES_H
struct myStruct_s; // fwd declaration
#endif

interface.cpp

#if defined(__CPPPLUS) || defined(__cplusplus) || defined (__CPLUSPLUS)
#include<TheRealFileContainingTheRealMyStruct_s.h>
// handle the .h's functions here
#endif

comeOutsideCppFile.cpp

#include "interface_types.h"
#include "interface.h"

void main()
{
    myStuct_s * x = CreateTheStruct;
    DoSomethingToTheStruct(x);
    DestroyTheStruct(x);
}

The above is a rough outline of how our stuff works, basically : Whatever the dll exposes needs to be : Created, Handled, Destroyed dll-side

This code is not 100% accurate!!!

Also, please keep in mind that if you're using a pure C++ dll, you probably need the same compiler with the same settings as the one used to build the dll.



回答3:

You might try looking to see if there are any formal C++ rules for what happens when an exception is thrown in one DLL and caught in another and then goes out of scope -- it seems very similar. For exceptions, I think you are required to provide a copy constructor with a special signature, though I'm unsure now exactly what it is.