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).

  • 相关阅读:
    APACHE 服务器开启URL REWRITE模块的方法
    tp路由+伪静态+去掉index.php
    PHP实现伪静态方法汇总
    在SAE分布式服务上开发需要解决的问题
    [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
    android 桌面小工具(Widget)开发教程
    VC++深入详解-第四章学习心得
    LED音乐频谱之输入数据处理
    CSharp设计模式读书笔记(15):命令模式(学习难度:★★★☆☆,使用频率:★★★★☆)
    CSharp设计模式读书笔记(14):职责链模式(学习难度:★★★☆☆,使用频率:★★☆☆☆)
  • 原文地址:https://www.cnblogs.com/cutepig/p/1927563.html
Copyright © 2011-2022 走看看