2 ms·
Which you do in C++ every time you use an &-reference.
by pcwalton 8y ago
Which you do in C++ every time you use an &-reference.
- saagarjha 8y agoBut that's not modern C++ anymore–the whole point is to have RAII manage your pointer's lifetime so it doesn't dangle.
- pcwalton 8y agoReferences are absolutely modern C++. There isn't a nontrivial C++ program in existence that doesn't use references.
- int_19h 8y agoYes, and most of those references are to locals that are passed as arguments to function calls that will never take their address. In other words, it's pass-by-ref - which is perfectly safe - not pointers. To obtain a reference to an object that is managed by a smart pointer, you would have to, at the minimum, explicitly dereference that pointer with *. In most cases, this also happens in an argument context, and the caller will maintain the pointer. It breaks when the pointer that's managing lifetime is a part of mutable state that the callee can touch somehow without returning - then it can cause the object to be freed while still holding a non-counting reference to it. At that point, you basically need something like Rust lifetime/ownership tracking to prevent such unsafe situations from happening.
- comex 8y agoTrue, but in this case the buggy code had to explicitly write "base::Unretained" because the callback system (base::BindOnce) performs additional checks.