[cfe-dev] Throwing exceptions from temporary object's destructor

John McCall rjmccall at apple.com
Mon Sep 3 14:06:37 PDT 2012

On Sep 2, 2012, at 10:46 PM, SENTHIL KUMAR THANGAVELU wrote:
> Hello all,
>    I had looked into bug 12306 http://llvm.org/bugs/show_bug.cgi?id=12306 specifically init-temp1.C (attached test case in mail). There is a problem with the following snippet, when compiled with clang 3.1 release
>      try {
>        b tmp(f (a(), a()));
>        g();
>      }  
>      catch (...) {}
>      In expression "b tmp(f (a(), a()));" if first temporary object a() throws an exception, object "tmp" is not cleaned up. Issue seems to be absence of ehcleanup entry for object "tmp" in the EhStack, when PopCleanupBlocks is called via VisitExprWithCleanups. Cleanup code for the 2 temporary "a()" objects are emitted when VisitExprWithCleanups node is visited, whereas autovar object "tmp"'s cleanup code is emitted by lexical scope cleaning. Association between the cleanup of temporary a() objects and the cleanup of auto var decl depending on the temporary objects is missing.
>      I tried pushing object "tmp" cleanup entry (kind == EHCleanup) on top of EhStack when a() objects' cleanup entries are also present in the EHStack. I did this to check whether cleanup code for freeing "tmp" will be emitted, when first temporary's destructor threw an exception. After my code change object tmp was destroyed when object a()'s destructor threw an exception. There were 2 problems with the generated code.
>      1) Second temporary object a() was destroyed before object "tmp" got destroyed
>      2) Apart from the destructor, even if the constructor of a() failed, call to cleanup "tmp" was emitted via landingpad

Right.  What needs to happen here for full correctness is that we need to flag that the cleanups were entered during an initialization and then have special logic in the cleanups-emission code to push an extra cleanup during the emission of the normal cleanup.  A very similar problem arises during returns, except there the extra cleanup needs to be conditional.

If you're willing to tackle this, that would be great.  Unfortunately, it's going to be a lot of subtle work.  That's why I haven't done it yet. :)


More information about the cfe-dev mailing list