Sep 30, 2016

[cppcon2016][c++] Leak-Freedom in C++ - note

CppCon 2016 - Herb Sutter “Leak-Freedom in C++... By Default.”-
https://goo.gl/2TNgQI
--
  • Ensure an object will be destroyed once it is no longer needed.
  • Correct by construction!
template<typename T>
using Pimpl = const unique_ptr<T>;
unique_ptr<data[]> ptr;


Double linked list:
  • a unique_ptr to the next
  • a raw ptr to the back
shared_ptr with alias constructor:
  • Do not violate layering.
  • Don't create ownership cycles across modules by owning 'upward' (violates layering)
  • Use weak_ptr to break cycles.

How?
  • Don't pass an owner down to 'unknow code' that might store it.
    e.g storing a shared_ptr
  • Simple.
    Use weak_ptr inside the call back callable object which point
    to outside resource.
ownership types:
  • 1 object, 1 owner:
    unique_ptr
  • 1 object , n owners:
    shared_ptr
  • N objects, 1 or n owners:
    deffered_ptr
Reachability is a property of the while group.
Not detectable from 1 object, or subgroup.

Idea:
heap for deffered_ptr is isolated.
Each module could have one isolated heap.
github.com/hsutter/gcpp

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.