Memory allocation and deallocation across dll boundaries

DLL边界内存管理
本文探讨了在使用相同编译器及CRT的情况下,如何在DLL边界安全地分配与释放STL类型如std::vector等的数据。文章通过具体示例介绍了如何正确实现跨模块对象的实例化与销毁,避免因内存管理不当导致的程序崩溃。
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).

 

转载于:https://www.cnblogs.com/cutepig/archive/2011/01/06/1927563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值