Re: 'new' keyword
- From: "James R. Twine" <spam@xxxxxxxxxxx>
- Date: 3 Aug 2005 07:51:12 -0700
The same unwinding will take place, yes, but consider that under normal
(non-placement) scenarios, "new" kinda works like this:
1: Allocate space for the object proper
2: Call constructor for object placed into allocated memory
2a: If an exception is thrown from the constructor, handle it, cleanup
the object, free the memory allocated for the object in step #1, and
re-throw the exception up the stack.
Now, suppose you are using "placement new" in a object cache that
manages larger blocks of memory and creates multiple objects in
"subblocks" of the larger blocks of memory. When a object is
requested, you either return an already-created one, or you have to
instantiate one in an available subblock. So you might have to manage
where your next free subblock is. Now, suppose you are constructing a
new object in a subblock, but its constructor raises an exception.
Before allowing it up the stack, you might have to adjust your "next
free subblock" location so that the subblock could be reused by the
next instantiation, and then do whatever other behaviour would be
required of your cache implementation, like eating the exception and
returning NULL, or re-throwing it back up the stack.
Besides, when doing an object cache like this, you usually
pre-construct a number of objects at once, so if you just allowed an
exception to propogate outward, you would not finish construction of
all of the objects in your pool/cache.
Peace!
-=- James.
.
- Follow-Ups:
- Re: 'new' keyword
- From: Alexander Grigoriev
- Re: 'new' keyword
- References:
- 'new' keyword
- From: Lisa Pearlson
- Re: 'new' keyword
- From: James R. Twine
- Re: 'new' keyword
- From: Alexander Grigoriev
- 'new' keyword
- Prev by Date: Re: Starting an action after a dialog has been drawn
- Next by Date: Re: Copy BSTR into TCHAR
- Previous by thread: Re: 'new' keyword
- Next by thread: Re: 'new' keyword
- Index(es):
Relevant Pages
|