BDE 4.14.0 Production release
|
Macros | |
#define | bslma_DeallocatorProctor bslma::DeallocatorProctor |
This alias is defined for backward compatibility. | |
Provide a proctor to conditionally manage a block memory.
bslma::DeallocatorProctor: proctor to conditionally manage a memory
This component provides a proctor class template, bslma::DeallocatorProctor
, to conditionally manage a block of (otherwise-unmanaged) memory. If not explicitly released, the managed memory is deallocated automatically when the proctor object goes out of scope by freeing the memory using the parameterized ALLOCATOR
(allocator or pool) supplied at construction. Note that after a proctor object releases its managed memory, the same proctor can be reused to conditionally manage another block of memory (allocated from the same allocator or pool that was supplied at construction) by invoking the reset
method.
The parameterized ALLOCATOR
type of the bslma::DeallocatorProctor
class must provide a (possibly virtual
) method:
to deallocate memory at the specified address
(originally supplied by the ALLOCATOR
object).
The bslma::DeallocatorProctor
is normally used to achieve exception safety in an exception neutral way by managing memory in a sequence of continuous memory allocations. Since each memory allocation may potentially throw an exception, an object of this proctor class can be used to (temporarily) manage newly allocated memory while attempting to allocate additional memory. Should an exception occur in subsequent memory allocation, the proctor's destructor deallocates its managed memory, preventing a memory leak.
This example illustrate a typical use of bslma::DeallocatorProctor
. Suppose we have an array class that stores an "in-place" representation of objects of parameterized TYPE
:
Note that the rest of the my_Array
interface (above) and implementation (below) is elided as the portion shown is sufficient to demonstrate the use of bslma::DeallocatorProctor
.
In order to implement the append
function, we first have to introduce an my_AutoDestructor
class
, which automatically destroy a sequence of managed objects upon destruction. See bslma::AutoDestructor
for a similar component with full documentation:
We can now continue with our implementation of the my_Array
class:
Both the use of bslma::DeallocatorProctor
and my_AutoDestructor
are necessary to implement exception safety.
The append
method defined above potentially throws in two places. If the memory allocator held in d_allocator_p
where to throw while attempting to allocate the new array of parameterized TYPE
, no memory would be leaked. But without subsequent use of the bslma::DeallocatorProctor
, if the allocator subsequently throws while copy constructing the objects from the old array to the new array, the newly allocated memory block would be leaked. Using the bslma::DeallocatorProctor
prevents the leak by deallocating the proctored memory automatically should the proctor go out of scope before the release
method of the proctor is called (such as when the function exits prematurely due to an exception).
Similarly, any resources acquired as a result of copy constructing the objects from the old array to the new array would be leaked if the constructor of TYPE
throws. Using the my_AutoDestructor
prevents the leak by invoking the destructor of the proctored (and newly created) objects in the new array should the my_AutoDestructor
goes out of scope before the release
method of the proctor is called.
Note that the append
method assumes the copy constructor of TYPE
takes an allocator as a second argument. In production code, a constructor proxy that checks the traits of TYPE
(to see whether TYPE
uses bslma::Allocator
) should be used (see bslalg::ConstructorProxy
).
#define bslma_DeallocatorProctor bslma::DeallocatorProctor |