Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Segfaults in assertHandlers

Moderators: kris

Posted: 11/21/07 08:15:58

Although for following code, first assertion failure is encountered and handled properly, but it results in a segmentation fault, after printing of message "An error occured in file...."

Is this behavior expected?

import tango.core.Exception;
import tango.io.Stdout;

void assertHandler( char[] file, size_t line, char[] msg = null )
{
    Stdout( "An error occurred in file " )( file )( ", line " )( line );
    if( msg )
        Stdout( ", with message:\n" )( msg );
    Stdout.newline;
}

void main()
{
    setAssertHandler( &assertHandler );
    assert( 'a' == 'b' );
    assert( 'c' == 'd', "'c' does not equal 'd'" );
    setAssertHandler( null );
    assert( 'e' == 'f' );
}
Author Message

Posted: 11/21/07 18:47:08

Unfortunately, yes. DMD assumes that an assertion failure will always throw so it does not generate a proper call stack for returning normally from an assertion handler. Apparently, the reason is to make code more optimal (though I fail to understand how something this small should matter for a debug build). In any case, I have asked Walter to change this and he refuses, so you can do whatever you want in your assert handler but it must exit via a throw.