[cap-talk] Composability and RAII
James A. Donald
jamesd at echeque.com
Sun Mar 6 14:33:11 PST 2011
On 2011-03-06 1:18 PM, David-Sarah Hopwood wrote:
> That is, the correctness of the application depends on choosing
> precisely when the object is disposed. For example, if a file handle is
> opened for writing, then closing the handle will flush buffered writes,
> and it is (almost always) important for that to be done at a particular
> Suppose that we have an outer object that is not a resource, but which
> holds a reference to an inner resource. Then I claim that either:
> a) the classification of the outer object as a non-resource was incorrect,
> because it does require explicit disposal, or
> b) the point at which the inner object should be disposed is not the
> point at which the outer object goes out of scope, but some earlier
> point in the lifetime of the outer object.
> In case a), the application needed to be aware of when the outer object
> should be disposed; it is wrong for it to be disposed implicitly by RAII
> (in the sense that it would lead to error-prone and unmaintainable code).
> In case b), RAII disposes the inner object too late.
If the resource is represented by a variable with particular scope, then
we ensure the scope of the resource with a single line of code, rather
than two lines quite distant from each other, which two lines have to be
kept in agreement, and probably will drift out of agreement as the code
gets bigger and bigger and more and more people work on the code.
More information about the cap-talk