<P> Another typical example is interacting with files: We could have an object that represents a file that is open for writing, wherein the file is opened in the constructor and closed when execution leaves the object's scope . In both cases, RAII ensures only that the resource in question is released appropriately; care must still be taken to maintain exception safety . If the code modifying the data structure or file is not exception - safe, the mutex could be unlocked or the file closed with the data structure or file corrupted . </P> <P> Ownership of dynamically allocated objects (memory allocated with new in C++) can also be controlled with RAII, such that the object is released when the RAII (stack - based) object is destroyed . For this purpose, the C + + 11 standard library defines the smart pointer classes std:: unique_ptr for single - owned objects and std:: shared_ptr for objects with shared ownership . Similar classes are also available through std:: auto_ptr in C + + 98, and boost:: shared_ptr in the Boost libraries . </P> <P> Both Clang and GNU Compiler Collection implement a non-standard extension to the C language to support RAII: the "cleanup" variable attribute . The following macro annotates a variable with a given destructor function that it will call when the variable goes out of scope: </P> <P> This macro can then be used as follows: </P>

Executed when a class goes out of scope