I use RAII any time it is even remotely possible. If I don't have a ready-made RAII mechanism for something, I will often write one. Over the years, I have built a pretty decent library of predicates for use with boost::shared_ptr<>, and I make extensive use of a modified version of Andrei Alexandrescu's ScopeGuard template classes.

// make sure pSubcategory is deleted at the end of scope, if guard is not dismissedScopeGuard guard = MakeGuard( DeleteSubcategoryPtr, pSubcategory );

And while I do most of my work in C# these days, I find ScopeGuard extremely useful in C# too, not so much for memory resources (thanks to garbage collection), but definitely for certain kinds of resources and especially for reciprocal operations. I ported the ScopeGuard class to C# and use it all the time, like this:

using (var transactionGuard = ScopeGuardFactory.MakeGuard(targetDbSqlTransaction.Rollback)) { // do some work on the DB that might throw an exception

djones wrote:Here's an example of how I use predicates with boost::shared_ptr<>

You don't really need a separate FindCloser class with shared_ptr, you can use FindClose() directly as the deleter. And the only thing that the FindCloser class is offering to shared_ptr is automatic assignment of the source variable to NULL. Which, honestly, I wouldn't really bother with in this situation:

Consider that the benefit of shared_ptr over unique_ptr (which you would need FindCloser for) is reference-counting across multiple shared_ptr instances (hence its name). Think of what happens if the source variable goes out of scope before all of the shared_ptr instances go out of scope. An invalid variable would be closed and assigned to NULL. Giving the shared_ptr its own copy of the HANDLE is safer.

If you intend to use the HANDLE locally and not pass it around, you should actually be using unique_ptr instead: