在模块(exe 和 dll)之间使用 STL(TR1)shared_ptr 是否安全

发布于 2024-07-09 17:01:07 字数 502 浏览 4 评论 0原文

我知道在一个模块中新建某些内容并在另一个模块中删除它通常会导致 VC++ 中出现问题。 不同运行时的问题。 如果我没记错的话,将模块与静态链接的运行时和/或动态链接的版本控制不匹配混合在一起都会搞砸。

但是,跨模块使用 VC++ 2008 的 std::tr1::shared_ptr 安全吗?

由于只有一个版本的运行时知道共享指针是什么,静态链接是我唯一的危险(目前...)。 我以为我已经读到了 boost 版本的 shared_ptr 可以安全地使用,但我正在使用 Redmond 的版本......

我试图避免对分配模块中的自由对象进行特殊调用。 (或者类似类本身中的“删除这个”)。 如果这一切看起来有点古怪,我将使用它进行单元测试。 如果您曾经尝试过对现有 C++ 代码进行单元测试,您就会明白您有时需要多么创造力。 我的内存是由 EXE 分配的,但最终将在 DLL 中释放(如果引用计数按照我认为的方式工作)。

I know that new-ing something in one module and delete-ing it in another can often cause problems in VC++. Problems with different runtimes. Mixing modules with staticly linked runtimes and/or dynamically linked versioning mismatches both can screw stuff up if I recall correctly.

However, is it safe to use VC++ 2008's std::tr1::shared_ptr across modules?

Since there is only one version of the runtime that even knows what what a shared_ptr is, static linking is my only danger (for now...). I thought I've read that boost's version of a shared_ptr was safe to use like this, but I'm using Redmond's version...

I'm trying to avoid having a special call to free objects in the allocating module. (or something like a "delete this" in the class itself). If this all seems a little hacky, I'm using this for unit testing. If you've ever tried to unit test existing C++ code you can understand how creative you need to be at times. My memory is allocated by an EXE, but ultimately will be freed in a DLL (if the reference counting works the way I think it does).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清醇 2024-07-16 17:01:08

我在一般主题上看到的最好建议是,内存应该在分配的同一上下文中释放。 但这并不排除库传回应用程序代码应该释放的指针,因此我想说您以这种方式传递共享指针可能是安全的,因为这是相同的一般情况。

如果系统的语义意味着指针实际上从 exe 转移(在所有权意义上)到 dll,那么 auto_ptr 可能是更好的解决方案。 但是,如果您的指针确实是共享的,那么shared_ptr可能是最好的解决方案。

The best advice I've seen on the general subject is that memory should be deallocated in the same context it's allocated. That doesn't preclude a library passing back a pointer that application code is supposed to free however, so I'd say you're probably safe passing the shared_ptr in this manner, as it's the same general situation.

If the semantics of your system mean that the pointer is actually transferred (in the ownership sense) from your exe to your dll, then an auto_ptr might be a better solution. If, however, your pointer is truly shared, then the shared_ptr is probably the best solution.

夏雨凉 2024-07-16 17:01:07

释放内存是安全的,只要它们都来自相同的内存管理上下文。 您已经确定了最常见的问题(不同的 C++ 运行时); 拥有单独的堆是您可能遇到的另一个不太常见的问题。

另一个你没有提到的问题,但共享指针可能会加剧这个问题,那就是当一个对象的代码存在于 DLL 中并由 DLL 创建时,但 DLL 外部的另一个对象最终引用了它(通过共享指针)。指针)。 如果该对象在 DLL 卸载后被销毁(例如,如果它是模块级静态,或者通过 FreeLibrary() 显式卸载 DLL,则共享对象的析构函数将崩溃。

这可以如果您尝试编写基于 DLL 的松散耦合插件,那就麻烦了。这也是 COM 让 DLL 决定何时卸载它们,而不是让 COM 服务器按需卸载它们的原因。

Freeing the memory is safe, so long as it all came from the same memory management context. You've identified the most common issue (different C++ runtimes); having separate heaps is another less-common issue you can run into.

Another issue which you didn't mention, but which can be exascerbated by shared pointers, is when an object's code exists in the DLL and is created by the DLL, but another object outside the DLL ends up with a reference to it (via shared pointer). If that object is destroyed after the DLL is unloaded (for example, if it's a module-level static, or if the DLL is explicitly unloaded by FreeLibrary(), the shared object's destructor will crash.

This can bite you if you attempt to write DLL-based, loosely-coupled plugins. It's also the reason that COM lets DLLs decide when they can be unloaded, rather than letting COM servers demand-unload them.

各空 2024-07-16 17:01:07

您开始看到 shared_ptr 是多么令人难以置信:)

跨越 DLL 边界的安全正是 shared_ptr 的设计初衷(当然,还有其他一些事情)。

与其他人所说的相反,在构造 shared_ptr 时,您甚至不需要传递自定义删除器,因为默认值已经类似于

template <typename T>
struct default_deleter {
    void operator()( T * t ) { delete t; }
};

并且

shared_ptr<Foo> foo( new Bar );

相当于

shared_ptr<Foo> foo( new Bar, default_deleter<Bar>() );

(即,不存在诸如shared_ptr 没有删除器)。

由于删除器上执行了类型擦除,所调用的delete始终来自于实例化shared_ptr的DLL。 ,绝不是 DLL 中最后一个 shared_ptr 超出范围的那个(即,调用删除器的 shared_ptr 将通过指向函数的指针来调用它)由原始的 shared_ptr 放在那里)。

将此与 auto_ptr 进行比较,后者将 delete 运算符直接嵌入到其(内联)析构函数中,这意味着 delete DLL destroys使用了auto_ptr,产生了与删除裸指针相同的问题。

通过相同的技术,始终保存在 shared_ptr 中的多态类甚至不需要虚拟析构函数,因为删除器将始终调用正确的析构函数,即使最后一个 shared_ptr code> 超出范围是为基类实例化的。

You're beginning to see how incredibly amazing shared_ptr is :)

Being safe across DLL boundaries is exactly what shared_ptr was designed to be (among other things, of course).

Contrary to what others have said, you don't even need to pass a custom deleter when constructing the shared_ptr, as the default is already something like

template <typename T>
struct default_deleter {
    void operator()( T * t ) { delete t; }
};

and

shared_ptr<Foo> foo( new Bar );

is equivalent to

shared_ptr<Foo> foo( new Bar, default_deleter<Bar>() );

(ie. there's no such thing as a shared_ptr without a deleter).

Because of the type erasure performed on the deleter, the delete that's called will always be the one from the DLL that instantiated the shared_ptr, never the one from the DLL where the last shared_ptr goes out of scope (ie. the shared_ptr invoking the deleter will call it through a pointer to a function put there by the original shared_ptr).

Compare this to auto_ptr, which embeds the delete operator directly in its (inline) destructor, which means that the delete of the DLL that destroys the auto_ptr is used, creating the same problems as deleting a naked pointer.

By the same technique, polymorphic classes that are always held in shared_ptrs don't even need a virtual destructor, because the deleter will always call the right destructor, even when the last shared_ptr to go out of scope is one instantiated for the base class.

酒与心事 2024-07-16 17:01:07

如果您担心,请使用带有删除器参数的shared_ptr构造函数的形式。 删除器可以回调分配该对象的模块,以便删除发生在正确的上下文中。

Boost 的文档声称它与 TR1 100% 兼容,因此希望这方面没有任何误导:

http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/shared_ptr.htm#constructors

If you're concerned, use the form of shared_ptr constructor that takes a deleter argument. The deleter can call back into the module that allocated the object so that the delete occurs in the proper context.

Boost's documentation claims it is 100% compatible with TR1, so hopefully there's nothing misleading about this:

http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/shared_ptr.htm#constructors

趁微风不噪 2024-07-16 17:01:07

我猜想它与跨模块使用 std 中的任何类一样安全。

也就是说:如果模块使用完全相同的运行时库以及完全相同的编译器开关和选项,那么应该是安全的。

切勿使用静态运行时库,因为每个模块都会获得其中所有全局变量的自己的实例。

I'd guess it is as safe as to use any of the classes in std across modules.

That is: It should be safe if the modules use exactly the same runtime library, and exactly the same compiler switches and options.

Never use the static runtime library, as each module will get their own instance of all globals within it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文