前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则R.37: 不要使用从破损的智能指针​获取的指针或引用

C++核心准则R.37: 不要使用从破损的智能指针​获取的指针或引用

作者头像
面向对象思考
发布2020-04-14 16:57:56
4980
发布2020-04-14 16:57:56
举报

R.37: Do not pass a pointer or reference obtained from an aliased smart pointer

R.37: 不要使用从破损的智能指针获取的指针或引用

Reason(原因)

Violating this rule is the number one cause of losing reference counts and finding yourself with a dangling pointer. Functions should prefer to pass raw pointers and references down call chains. At the top of the call tree where you obtain the raw pointer or reference from a smart pointer that keeps the object alive. You need to be sure that the smart pointer cannot inadvertently be reset or reassigned from within the call tree below.

违反本规则是引用计数丢失和发生悬空指针的第一号理由。函数更应该沿着调用链向下传递原始指针和引用。你应该在调用树的最顶端,从可以保证对象存在的智能指针获得原始指针或引用。你需要保证智能指针不会在调用树的下面被不小心重置或者重新赋值。

Note(注意)

To do this, sometimes you need to take a local copy of a smart pointer, which firmly keeps the object alive for the duration of the function and the call tree.

要做到这点,你需要获取一个智能指针的局部拷贝,通过它可以在函数和调用树执行期间将对象牢牢地锁定。

Example(示例)

Consider this code:

考虑以下代码:

// global (static or heap), or aliased local ...
shared_ptr<widget> g_p = ...;

void f(widget& w)
{
    g();
    use(w);  // A
}

void g()
{
    g_p = ...; // oops, if this was the last shared_ptr to that widget, destroys the widget
}

The following should not pass code review:

下面的代码应该无法通过代码评审:

void my_code()
{
    // BAD: passing pointer or reference obtained from a non-local smart pointer
    //      that could be inadvertently reset somewhere inside f or its callees
    f(*g_p);

    // BAD: same reason, just passing it as a "this" pointer
    g_p->func();
}

The fix is simple -- take a local copy of the pointer to "keep a ref count" for your call tree:

为了改正这个问题--获取指针的局部拷贝以便为调用树“保持引用计数”。

void my_code()
{
    // cheap: 1 increment covers this entire function and all the call trees below us
    auto pin = g_p;

    // GOOD: passing pointer or reference obtained from a local unaliased smart pointer
    f(*pin);

    // GOOD: same reason
    pin->func();
}
Enforcement(实施建议)
  • (Simple) Warn if a pointer or reference obtained from a smart pointer variable (Unique_pointer or Shared_pointer) that is non-local, or that is local but potentially aliased, is used in a function call. If the smart pointer is a Shared_pointer then suggest taking a local copy of the smart pointer and obtain a pointer or reference from that instead.
  • (简单)如果函数调用时使用了一个从非局部智能指针变量(Unique_pointer or Shared_pointer)获取的指针或者引用,报警。智能指针是局部变量但是可能是别名时也报警。如果智能指针是一个Shared_pointer,建议获取一个智能指针的局部拷贝然后从该拷贝获取指针或引用。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r37-do-not-pass-a-pointer-or-reference-obtained-from-an-aliased-smart-pointer


觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R.37: Do not pass a pointer or reference obtained from an aliased smart pointer
  • Reason(原因)
    • Enforcement(实施建议)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档