什么时候用智能指针包装来自工厂的遗留原始指针是安全的?

发布于 2025-01-09 00:46:36 字数 596 浏览 3 评论 0原文

在 c++11 之前开始开发 C++ 框架工厂返回的原始指针,什么时候可以安全地包装在智能指针(unique_ptr 或 share_ptr)中?

例如,当所有事情都发生在同一个函数中时:

int main(int argc,char** argv)
 {
  //.....
  auto* runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
  //... many lines later
  delete runManager;
 }

它认为将返回的原始指针放入 unique_ptr 中并在函数末尾取出删除是无害的。

但除了这个特定情况之外,我不确定是否还有其他情况可以安全地完成此操作 - 并且您的代码在可读性/可维护性方面的增益值得付出努力。

特别是,我正在使用一个 C++ 框架工具包,该工具包开发了自己的跟踪和处理框架创建的对象的方法,因此在以非最初预期的方式使用返回的指针之前,我应该三思而后行(或更多)。

任何建议都非常受欢迎。

When is safe to wrap in smart pointers (unique_ptr or shared_ptr) the raw pointers returned by factories of C++ frameworks started to develop before c++11?

For example when everything happen within the same function:

int main(int argc,char** argv)
 {
  //.....
  auto* runManager = G4RunManagerFactory::CreateRunManager(G4RunManagerType::Default);
  //... many lines later
  delete runManager;
 }

It believe that it is harmless to put the returned raw pointer in a unique_ptr and take out the delete at the end of the function.

But besides this specific case, I am not sure if there are other cases in which this can be done safely - and your code gain in readability / maintainability is worth the effort.

In particular I am using a C++ framework toolkit that developed its own way of tracking and disposing the objects created by the framework, so I should have to think twice (or more) before using the returned pointers in a way not originally intended.

Any suggestion is very welcome.

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

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

发布评论

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

评论(1

别闹i 2025-01-16 00:46:36

仅当返回值所指向的对象的所有权转移给用户(即编写调用该工厂的代码的您)时。工厂返回的对象可能实际上由框架拥有,并且会被正确销毁并且(几乎)永远不会丢失。在这些上使用智能指针会导致问题,主要是重复删除。

Qt 框架就是一个这样的例子,其中所有视觉元素在被销毁时都会销毁其子元素,并且所有 QObject 元素都被“枚举”。那里的工厂对用户来说是隐藏的,主要与信号槽系统和元对象数据相关。在某些特殊情况下可以使用智能指针,但 Qt 提供了自己的风格。

不拥有已创建对象的工厂可能需要一些自定义步骤来直接删除对象,在这种情况下,您应该为智能指针使用适当的删除器。

关于现代 C++ 中“原始指针不好”的说法存在一些误解。它实际上必须表述为“原始拥有指针是不好的”。如果您丢失了原始指针的值,并且程序中没有任何内容可以存储其值以进行正确的重新分配和释放资源,则它是一个拥有指针。

Only when ownership of object pointed by returned value is transferred to user (i.e. you, who write the code calling that factory). An object returned by factory might be actually owned by framework and would be destroyed properly and (almost) never lost. Using smart pointer on those would result in problems, mainly double deletion.

An example of such is Qt framework, where all visual elements destroy their children when they are destroyed and all QObject elements ar "enumerated". A factory there is hidden from user and mostly related to signal-slot system and metaobject data. There are special cases where smart pointers can be used, but Qt provides own flavor of those.

A factory that doesn't own created objects may require some custom steps to delete object directly, in that case you should use proper deleters for smart pointer.

There is a little misunderstanding about statement that in modern C++ "raw pointers are bad". It actually must be worded "raw owning pointers are bad". If you loose value of raw pointer and there is nothing in program that stores its value for purpose of proper deallocation and releasing of resources, it's an owning pointer.

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