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

面向对象思考

共 2607字,需浏览 6分钟

 ·

2020-04-12 23:22

d3a7344649a8708246e8bdb0d273dade.webp

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 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




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

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

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


浏览 22
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报