13.4. Resource Management

Many applications acquire and release resources (free store memory, files, etc.) with class objects. How do we prevent resource leaks when exceptions occur? This section explores techniques that help manage resources during exception handling. We'll also discuss strategies for throwing exceptions from constructors and destructors. These techniques will improve the designs of your systems and make them more robust with exceptions.

The auto_ptr Class

Using operator new with classes that throw exceptions requires more discipline than using operator new with classes that don't throw exceptions. Consider instantiating a Dbase object, for instance, that throws DbaseError exceptions (see Listing 13.7 on page 616).

 try { Dbase ...

Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.