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

查看:21
本文介绍了(静态链接的)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 是否映射到主程序的同一个进程空间?(我很确定这里的答案是肯定的,否则将指针从主程序中的函数传递到 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,但我与存根库链接

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

推荐答案

DLL/exe 将需要链接到 C 运行时库的实现.

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

对于 C Windows 运行时库,如果您希望链接到以下内容,您可以选择指定:

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. 更多(您可以查看链接)

它们中的每一个都将引用不同的堆,因此您不能将从一个运行时库的堆获得的地址传递给另一个.

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.

现在,这取决于您所说的 DLL 已链接到哪个 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 运行时库的更多信息 此处 &此处

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

引用自 MSDN:

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

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

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