3 ms·
Properly executed RAII, at least in C++, should never break in the two cases you mention. The RAAI object should be non-copyable (no copy constructor in C++),
by unscaled 11y ago
Properly executed RAII, at least in C++, should never break in the two cases you mention.
The RAAI object should be non-copyable (no copy constructor in C++), so the only way it can be passed by value to a closure is with an explicit move operation, which is not something you would do on mistake. The original copy would now revert to a proper "uninitialized" or "closed" state. Debuggers too are usually well aware of out-of-scope objects, so I don't see the point here.
As for complex scenarios, they are probably less than 1% of the time you'd use RAAI. When you do have complex scenarios that need techinques like caching and pooling - well, yeah, RAAI won't solve them on its own, but the same goes for macros and closures.
Besides that, most RAAI uses just can't be replaced by closures, since you cannot break a loop or return a value from within a closure, for instance (Ruby procs can do that, but there's no equivalent to that in most languages). And both a closure-based and a macro-based solution quickly become an unwieldy nest-fest once you need to deal with multiple resources in a row.
- rbehrends 11y ago> Properly executed RAII, at least in C++, should never break in the two cases you mention. So, std::shared_ptr (or anything else that does reference counting) is not properly executed RAII? > Debuggers too are usually well aware of out-of-scope objects, so I don't see the point here. I wasn't talking about debuggers, but code that is enabled for debugging purposes. E.g. globally remembering the last N objects passed to a function. My larger point is that either lifetime or reachability is off. You either have a resource that can still be accessed through a stale handle even though it's been deallocated or one that remains allocated past its intended lifetime.