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

  • 相关阅读:
    ActiveMQ的消息模式——队列模式(Queue)
    在foxmail上添加阿里邮箱
    Neither the JAVA_HOME nor the JRE_HOME environment variable is defined 错误解决
    博客园首页新随笔联系管理订阅订阅随笔- 89 文章- 0 评论- 3 Centos7开放及查看端口
    tomcat设置为开机自启动
    Tensorflow2疑难问题---2、tensorflow2.3的GPU版本安装
    Tensorflow2疑难问题---1、课程介绍
    tensorflow2的gpu的版本安装(一些核心点)
    此环境变量太大, 此对话框允许将值设置为最长2047个字符(解决方法)
    windows下cuda的安装
  • 原文地址:https://www.cnblogs.com/cutepig/p/1927563.html
Copyright © 2011-2022 走看看