zoukankan      html  css  js  c++  java
  • 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).

  • 相关阅读:
    Xshell学习第九课:数组与字符串
    Xshell学习第八课:函数
    Xshell学习第七课:sed语句
    Xshell学习第六课:read与for语句循环
    Xshell学习第五课:if判断语句
    Xshell学习第四课:grep与正则表达式
    Xshell学习第三课:编程原理
    Xshell学习第二课:重定向和管道符
    iOS中坐标转换
    iOS工作笔记(十五)
  • 原文地址:https://www.cnblogs.com/cutepig/p/1927563.html
Copyright © 2011-2022 走看看