Notes
Slide Show
Outline
1
Resource Management in C++
  • An alternative to garbage collection


  • Bartosz Milewski
  • www.relisoft.com
2
RAI in ARM
  • Resource Acquisition Is Initialization
  • Ellis, Stroustrup
    “The Annotated C++ Reference Manual” Addison-Wesley 1990
3
Bad Code!
4
Code Quality
  • 1. Readability. Easy to read and understand. Not obscured by lots of inessential bookkeeping. Easy to factorize and assimilate one concept at a time.
  • 2. Verifiability. Reasonably easy to verify correctness. Locality—you shouldn't have to refer to many separate pieces of code to prove correctness. Easy to debug.
  • 3. Maintainability. Easy to modify—it shouldn't be fragile. A small modification shouldn't make it fail in unexpected ways. The creator or the maintainer should have as few opportunities as possible for making a mistake.
5
Bad Code In C
6
Transplanting Bad Code to C++
7
Good C++: Critical Section
8
Good C++: Lock
9
Good Clean C++: Usage
10
Resources
  • Resource: “something” that must be released after it’s acquired
    • Memory
    • Heap objects created using new
    • Handles: critical section, file, device context
    • States: lock, reference count, progress UI (hourglass cursor)
    • Objects: recursively
11
Resource Owners
  • Owner: “something” that is responsible for the release of the resource
    • Scopes (global, local)—automatic destruction of stack objects and global/static objects
    • Objects: recursively—automatic destruction of sub-objects
  • Built-in ownership mechanisms
    • Destruction at the end of the scope
    • Stack unwinding during exceptions
    • Recursive destruction of sub-objects

12
First Rule of Acquisition
  • Idea: make use of built-in mechs
  • Acquire resources in constructors (one resource per object)
  • Release resources in destructors
13
Heap Objects
  • Note that heap objects are resources, so they must always have an owner object
  • class Foo
    {
    public:
        Foo () : _bar (new Bar) {}
        ~Foo () { delete _bar; }
    private:
        Bar * _bar;
    };


14
Proof of correctness
  • Gurantee that all resources have owners -> no resource leaks
    • Raw resources owned by objects
    • Objects owned by other objects and scopes
    • Each scope is eventually exited, q.e.d.
    • Note: Heap objects (allocated using new) are resources—must have owners
  • Locality of proof—constructors/destructors


15
Ownership transfer mechanisms
  • Built-in object transfer
    • Returning objects by value
  • Built-in object cloning
    • Copy construction
    • Assignment
    • Passing arguments by value (a combination of the two)
  • Copy constructors and operator= provide hooks to control transfers
16
Ownership Transfer in RM
  • Idea: make use of built-in mechs
  • Pass resources inside transfer objects with value-like semantics
    • Example std::auto_ptr (move semantics)
  • Ownership policies
    • Disallow transfer: private copy-co/assignment
    • Exclusive: auto_ptr-like
    • Reference counted


17
Heap Resource Management
  • Equivalent to Garbage Collection
  • 99% can be solved by auto_ptr
  • Allocation of heap resources
    • auto_ptr<Foo> foo (new Foo);
    • foo.reset (new Foo);
    • : _foo (new Foo) // constructor
  • Ownership transfer
    • auto_ptr<Foo> MakeFoo ();
    • void EatFoo (auto_ptr<Foo> foo);
18
Container for auto_ptr
  • Standard containers don’t work with auto_ptr
  • Standard containers work with ref-counted smart pointers (Alexandrescu)
    • Must use RC pointers all over (-)
    • No support in the standard yet (-)
    • Overhead (-)
    • Works with all containers (+)
19
Auto_vector (Milewski)
  • Works with std::auto_ptr (big +)
  • Little overhead (+)
  • Must be generalized to other containers (-) [less needed as it might seem]
  • Not supported by the standard (-)
20
Auto_vector
  • A vector of pointers with modified interface and semantics
21
Auto_vector: lvalue
22
auto_lvalue
23
RM Garbage Collection
  • Each heap object has an auto_ptr or auto_vector owner at all times
  • Each polymorphic class has a virtual destructor
  • Makes a very dependable cheap garbage collection mechanism
  • But there is more!


24
Other resources
  • Traditional garbage collection is useless with other resources
  • Critical section and Lock example
  • Windows resources
    • Handles
    • Files, memory mapped files
    • Threads
    • Etc.
25
Window Handle
26
Windows AutoHandle
27
Move Semantics
28
Default Disposal Policy
  • Dipose is to be specialized
29
Icon
30
GDI Handle
31
Pen
32
Pen Maker
33
Device Context Handle
34
HDC Owners: PaintCanvas
35
HDC Owners: UpdateCanvas
36
Conclusion
  • RM revolutionized our own work at Reliable Software
  • Easy to enforce and maintain
    • Global searches for new and delete
    • Global searches for virtual
  • Our own Windows library using RM
  • More at www.ReliSoft.com