[cfe-dev] [Bug] Wrong Exception Handling : Destructor not called immediately

John McCall rjmccall at apple.com
Mon Apr 21 13:47:43 PDT 2014


On Apr 21, 2014, at 12:21 PM, suyog sarda <sardask01 at gmail.com> wrote:
> Ok. sorry for my silly question above. i tested the code for the question i had, and it seems that destructor of all temporaries 
> as well as other objects created before the current line are being called when exception occurs, except the destructor of the 
> outermost object. 
> 
> From the discussion in the link shared in the previous mail and the results of the test case, i conclude that for 
> clang, outermost object temp of class b is not considered constructed until all the inner temporaries are destroyed.
> 
> Since the outermost 'temp' is considered yet to be constructed fully, its destructor is not pushed in 'EHStack'. 
> As a result whenever exception occurs in the destructor of temporaries, the EHStack does not contain destructor of outermost
> object and hence its cleanup doesn't take place. 
> 
> Please correct me if i am wrong? Also, while debugging i came across, following piece of code,
> 
> void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
>         EnterCXXTryStmt(S);
>         EmitStmt(S.getTryBlock());
>         ExitCXXTryStmt(S);
>  }
> 
>  
> which after several function calls in between leads me to below (for init-temp1.C test code) :
> 
> CodeGenFunction::emitAutoVarTypeCleanup
> {
> .....
> // Use an EH cleanup in array destructors iff the destructor itself
>   // is being pushed as an EH cleanup.
>   bool useEHCleanup = (cleanupKind & EHCleanup);
>   EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
>                                      useEHCleanup);
> }
> 
> At this point the destructor is pushed to EHStack. When i traced the code for init-temp1.C, i hit the EHStack.pushcleanup function above, still the destructor of outermost object is not being called. 
> 
> Can someone please help me if i am looking at right place? If my analysis for clang is right - outermost object not considered created until innermost temporaries are destroyed - where should i look to correct this problem?
> 
> If by design, clang doesn't consider outermost object to be created until inner temporaries are destroyed, would this be a resource leak (i don't think it is leak, as the object is yet to be constructed, hence no destruction required)?
> 
> Your help would be highly appreciated.  

If the constructor has completed, the destructor needs to be called later.  IIRC, the problem that makes solving this so annoying is that, as soon as one temporary destructor terminates abnormally, the next thing to destroy is always the local variable (since it was constructed most recently), and only then proceeding to destroy the remainder of the temporaries.

That is, if we’re creating local variable X, and we needed to construct temporaries A, B, and C to do that, then if C’s destructor throws, we need to destroy X, then B, then A; and if B’s destructor throws, we need to destroy X before A.

This is not achievable with a simple stack mechanism.

John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140421/e4e29f28/attachment.html>


More information about the cfe-dev mailing list