Do(静态链接)DLL使用与主程序不同的堆? [英] Do (statically linked) DLLs use a different heap than the main program?

查看:148
本文介绍了Do(静态链接)DLL使用与主程序不同的堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Windows编程的新手,我刚刚迷失了两小时,狩猎一个大家似乎都知道的错误:你不能在一个DLL中的堆上创建一个对象,并在另一个DLL(或主要程序)。

I'm new to Windows programming and I've just "lost" two hours hunting a bug which everyone seems aware of: you cannot create an object on the heap in a DLL and destroy it in another DLL (or in the main program).

我几乎肯定在Linux / Unix这不是这种情况(如果是,请说,但我很确定我做了这么几千次没有问题...)。

I'm almost sure that on Linux/Unix this is NOT the case (if it is, please say it, but I'm pretty sure I did that thousands of times without problems...).

此时我有几个问题:

1)静态链接的DLL使用与主程序不同的堆栈?

1) Do statically linked DLLs use a different heap than the main program?

2)静态链接的DLL是否映射在主程序的相同进程空间? (我确定这里的答案是大的YES,否则它不会有意义的将主程序中的函数传递给DLL中的函数)。

2) Is the statically linked DLL mapped in the same process space of the main program? (I'm quite sure the answer here is a big YES otherwise it wouldn't make sense passing pointers from a function in the main program to a function in a DLL).

我正在谈论纯/常规DLL,而不是COM / ATL服务

I'm talking about plain/regular DLL, not COM/ATL services

编辑:静态链接我的意思是我不使用LoadLibrary加载DLL但我链接到stub库

By "statically linked" I mean that I don't use LoadLibrary to load the DLL but I link with the stub library

推荐答案

DLLs / exes将需要链接到C运行时库的实现。

DLLs / exes will need to link to an implementation of C run time libraries.

如果您使用C Windows Runtime库,则可以选择指定,如果您希望链接到以下内容:

In case of C Windows Runtime libraries, you have the option to specify, if you wish to link to the following:


  1. 单线程C运行时库(现在已经停止支持单线程库)

  2. 多线程DLL /多线程调试DLL

  3. 静态运行时库。

  4. 更多(您可以查看链接)

  1. Single-threaded C Run time library (Support for single threaded libraries have been discontinued now)
  2. Multi-threaded DLL / Multi-threaded Debug DLL
  3. Static Run time libraries.
  4. Few More (You can check the link)

他们中的每一个都将被引用到不同的堆,所以你不允许从一个运行时库的堆获取的地址到其他。

Each one of them will be referring to a different heap, so you are not allowed pass address obtained from heap of one runtime library to other.

现在,这取决于哪个C运行时库你正在谈论已经被链接到。假设我们假设你正在使用的DLL链接到静态C运行时库,你的应用程序代码(包含主函数)已经链接到多线程C运行时DLL,那么如果你传递一个指向内存分配的指针DLL到您的主程序,并尝试将其释放,反之亦然,可能导致未定义的行为。因此,基本的根本原因是C运行时库。请仔细选择它们。

Now, it depends on which C run time library the DLL which you are talking about has been linked to. Suppose let's say, the DLL which you are using has been linked to static C run time library and your application code (containing the main function) has linked to multi-threaded C Runtime DLL, then if you pass a pointer to memory allocated in the DLL to your main program and try to free it there or vice-versa, it can lead to undefined behaviour. So, the basic root cause are the C runtime libraries. Please choose them carefully.

请在C运行时库上查找更多信息 here & here

Please find more info on the C run time libraries supported here & here

MSDN的报价:

注意 不要混合运行的静态和动态版本时间库在一个进程中拥有多个运行时库的副本可能会导致问题,因为一个副本中的静态数据不会与其他副本共享。链接器可以阻止您在一个.exe文件中与静态和动态版本链接,但您仍然可以使用两个(或更多)运行时库的副本。例如,与运行时库的静态(非DLL)版本链接的动态链接库可能会与使用动态(DLL)版本的运行时库相关联的.exe文件使用时导致问题。 (您也应该避免在一个进程中混合库的调试和非调试版本。)

这篇关于Do(静态链接)DLL使用与主程序不同的堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆