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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.