Re: 'new' keyword

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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.

.



Relevant Pages

  • Re: Allocators and exceptions
    ... of an object never propagates an exception, ... I guess I had misunderstood what's meant by 'constructor', ... the leak in this case doesn't bother me too much, ... very long-running applications are likely to have problems with memory ...
    (comp.lang.ada)
  • Re: is such exception handling approach good?
    ... here if you get a memory exception in m_pConn you get a memory leak in ... m_pBuff since the dtor will not be called. ... has been created you can't clean it up in your exception handler. ... of them in constructor; sometimes all of them. ...
    (microsoft.public.vc.language)
  • Re: new keyword
    ... all required memory (what happens or which exception is thrown depends ... configured to have "new" return NULL if it fails to allocate. ... by the underlying object's constructor will be destructed, ... memory allocated by "new" will be freed. ...
    (microsoft.public.vc.mfc)
  • Re: exiting a constructor early
    ... what exactly happens to any dynamically allocated memory that was ... | obtained before the exception was thrown? ... but for memory that your object attempts to allocate in the constructor, ... you would have to somehow handle that memory in an appropriate handler, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Readings on clr optimization?
    ... The compiler cannot inline a call to a constructor if the class has a static ... Weak references are special objects that allow you to track references that ... There aren't many implication with memory ...
    (microsoft.public.dotnet.framework.clr)