Memory allocation and deallocation across dll boundaries.

Memory allocation and deallocation across dll boundaries

http://stackoverflow.com/questions/1344126/memory-allocation-and-deallocation-across-dll-boundaries
http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/320c4868-2194-44f5-a241-d5fb37ff0738

STL and std::string memory allocation across a DLL boundary
http://forums.devx.com/showthread.php?t=89083

Allocating and freeing memory across module boundaries
http://blogs.msdn.com/b/oldnewthing/archive/2006/09/15/755966.aspx

Instancing/Deleting objects in DLL modules: Assertion errors on delete.
http://www.gamedev.net/community/forums/topic.asp?topic_id=584050

example:

////////////////////////////dll:

//export.h
#pragma once

#if defined( APP_EXPORT ) || defined( PR_HOST_SIDE_COMM )
#define    ASMVISION_EXPORT
#endif    // APP_EXPORT

#if defined( ASMVISION_EXPORT )
#define    ASMVISION_XXPORT    __declspec(dllexport)
#else
#define    ASMVISION_XXPORT    __declspec(dllimport)
#endif    //    ASMVISION_EXPORT

//mydll.h:

#pragma once
#include "export.h"
#include <vector>

struct ASMVISION_XXPORT MyObj
{
    std::vector<int> v;
};

ASMVISION_XXPORT void GetMyObj(MyObj &obj);

//mydll.cpp
#include "mydll.h"

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}

void GetMyObj(MyObj &obj)
{
    obj.v.resize( 1000 );
}

//////////////////////exe
#include <mydll.h>

int _tmain(int argc, _TCHAR* argv[])
{
    MyObj obj;
    
    GetMyObj(obj);
    
    return 0;
}

如果将红色的ASMVISION_XXPORT删除,会发现dll不会导出MyObj的如下函数
MyObj::MyObj(struct MyObj const &)
MyObj::MyObj(void)
struct MyObj & MyObj::operator=(struct MyObj const &)
MyObj::~MyObj(void)
在这种情况下,exe销毁MyObj时,调用的是exe中的~vector<>而非该dll中的destructor,从而导致crash

 

 

As long as you are:
 
1) using one common compiler (like using only VC++ 2005, or using only VC++ 2008), **AND**
2) linking to the same CRT everywhere (like all projects use the CRT multi-threaded DLL, or all are using the CRT static lib, and there is no mixing, like one project is using the "debug" CRT dll, and another is using "release" CRT dll)...

...then you will be able to pass around STL types as if they were native types.  You will have no need for serialization and all that mess.  The same (mostly) applies to Boost.   If you switch from an older to newer boost deployment you should be fine, as long as the correspondig header files you are using have not changed between the two releases.  If the boost header files have changed, then you should recompile everything that uses the new headers (and make sure that your entire application uses just one version of boost).

 

Powered by Jekyll and Theme by solid

本站总访问量