[LLVMdev] [LLVMDev] Object in a try-catch block not being destroyed even after an exception

SHIVA PRASAD shivahms at gmail.com
Tue Jun 19 04:14:46 PDT 2012


Dear LLVM Members,

        The below source code from gcc test suite replicates the problem
which i am facing.  When it is built with clang, it throws an abort
(highlighted).
The following are my observations -

1. The object(tmp) is not getting destroyed immediately after the exception
is thrown.  In case of Clang, the object is getting destroyed at a later
point of time.
2. In case of gcc, the scope of the object created inside the try-catch
block is limited to the block.  Once the exception is thrown, the
object(tmp) is destroyed.
3. The assembly files for both clang and gcc were generated and compared,
wherein the sequence of operations look similar.
4. I also tried the MS-Visual Studio compiler, which gives a result similar
to gcc.

As per my understanding of the C++ standard, the object has to be destroyed
as soon as the exception is thrown.
Please let me know which is the correct behavior and suggest me the changes
required.


Thanks & Regards,
Shivaprasad
shivahms at gmail.com

/******************************************************************Source
Code
Start*******************************************************************/
extern "C" void abort ();

int thrown;

int as;
struct a {
  a () { ++as; }
  ~a () { --as; if (thrown++ == 0) throw 42; }
};

int f (a const&) { return 1; }
int f (a const&, a const&) { return 1; }

int bs;
int as_sav;
struct b {
  b (...) { ++bs; }
  ~b ()   { --bs; as_sav = as; }
};

bool p;
void g()
{
  if (p) throw 42;
}

int main () {
  thrown = 0;
  try {
    b tmp(f (a(), a()));

    g();
  }
  catch (...) {}

  // We throw when the first a is destroyed, which should destroy b before
  // the other a.
  if (as_sav != 1)
    abort ();

  thrown = 0;
  try {
    b tmp(f (a()));

    g();
  }
  catch (...) {}

  if (bs != 0)
    abort ();
}
/******************************************************************Source
Code End*******************************************************************/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120619/ed14ef5a/attachment.html>


More information about the llvm-dev mailing list