mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 12:44:42 +03:00
remove weird characters in R.38 examples
This commit is contained in:
@@ -6223,21 +6223,21 @@ You need to be sure that smart pointer cannot be inadvertently be reset or reass
|
|||||||
|
|
||||||
**Example**: Consider this code:
|
**Example**: Consider this code:
|
||||||
|
|
||||||
// global (static or heap), or aliased local...
|
// global (static or heap), or aliased local...
|
||||||
shared_ptr<widget> g_p = ...;
|
shared_ptr<widget> g_p = ...;
|
||||||
|
|
||||||
void f( widget& w ) {
|
void f( widget& w ) {
|
||||||
g();
|
g();
|
||||||
use(w); // A
|
use(w); // A
|
||||||
}
|
}
|
||||||
|
|
||||||
void g() {
|
void g() {
|
||||||
g_p = ... ; // oops, if this was the last shared_ptr to that widget, destroys the widget
|
g_p = ... ; // oops, if this was the last shared_ptr to that widget, destroys the widget
|
||||||
}
|
}
|
||||||
|
|
||||||
The following should not pass code review:
|
The following should not pass code review:
|
||||||
|
|
||||||
void my_code() {
|
void my_code() {
|
||||||
f( *g_p ); // BAD: passing pointer or reference obtained from a nonlocal smart pointer
|
f( *g_p ); // BAD: passing pointer or reference obtained from a nonlocal smart pointer
|
||||||
// that could be inadvertently reset somewhere inside f or it callees
|
// that could be inadvertently reset somewhere inside f or it callees
|
||||||
g_p->func(); // BAD: same reason, just passing it as a "this" pointer
|
g_p->func(); // BAD: same reason, just passing it as a "this" pointer
|
||||||
@@ -6246,7 +6246,7 @@ The following should not pass code review:
|
|||||||
The fix is simple -- take a local copy of the pointer to "keep a ref count" for your call tree:
|
The fix is simple -- take a local copy of the pointer to "keep a ref count" for your call tree:
|
||||||
|
|
||||||
void my_code() {
|
void my_code() {
|
||||||
auto pin = g_p; // cheap: 1 increment covers this entire function and all the call trees below us
|
auto pin = g_p; // cheap: 1 increment covers this entire function and all the call trees below us
|
||||||
f( *pin ); // GOOD: passing pointer or reference obtained from a local unaliased smart pointer
|
f( *pin ); // GOOD: passing pointer or reference obtained from a local unaliased smart pointer
|
||||||
pin->func(); // GOOD: same reason
|
pin->func(); // GOOD: same reason
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user