Perhaps this is just an example of the code this applies to, but I don't think the user should assume that ptr valid when the constructor of X throws. It can also be reset before ref is assigned.
So, I would say, clean the inner object if you can (that is, if the front of the arena is at the end of the inner object). OK, the selection from the arena becomes invalid, which is not normal, but it is a distribution that should never have left the real world.
Perhaps you could make this explicit, with the concept of soft distribution. He is not guaranteed to live forever, because as yet "soft", he can be released back into the arena. Then constructor X would do something like:
SoftPtr<std::string> tmp(ptr->SoftAllocate<std::string>()); stuff_that_might_throw(); ref = tmp.release();
Executing the SoftPtr destructor without first calling release means that the object reference was not shown. It calls the MemoryArena function, which does something like:
- destroy object
- check if this is the last distribution from the arena.
- if so, subtract the size from the current arena position indicator
- if not, do nothing (memory leak)
Thus, any number of sections can be "canceled" if it is performed in the reverse order.
Steve Jessop Dec 21 '11 at 18:16 2011-12-21 18:16
source share