scope(exit) & stack => double free or corruption (fasttop) ... help?

Charles Hixson charleshixsn at earthlink.net
Sun Feb 24 15:14:01 PST 2013


Given a struct with:

~this()
{  close();	}

void	close()
{  if	(currentKey !is null)	currentKey	=	null;
    if	(cursor is null)	return;
    tcbdbcurdel(cursor);
}

and:

   scope (exit)	if (bdb !is null)  tcbdbclose(bdb);
   //scope(exit)	cur.close;	   // <<- cur is the struct noted above

Why does the second scope need to be commented out to avoid:
*** glibc detected *** ./tctest: double free or corruption (fasttop): 
0x00000000015a5980 ***

I thought that scope(exit) statements were executed in the reverse of 
the order that they were executed.

FWIW, it's *probably* safe to leave it commented out, but I'm not really 
sure because it depends on the internals of a C library that I've been 
considering opaque.
N.B.:  If I manually insert the cursor.close method at the end of the 
routine, it works as expected.  The problem only occurs if I expect 
scope(exit) to issue the close.  (I suppose I could just add another 
level of parens to force the order, but should I need to?)


More information about the Digitalmars-d-learn mailing list