www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - setAssertHandler (druntime) segfaults

reply Lutger <lutger.blijdestijn gmail.com> writes:
There is a function setAssertHandler in druntime, but when I try to use it 
it segfaults. I'm not sure how it should be used, this is a complete example 
of what I try to do:

import std.stdio;
import core.exception;

void handleAssertion(string file, size_t line, string msg = null)
{
    writefln("assert in %s at line %s", file, line);
};

static this()
{
    setAssertHandler( &handleAssertion  );
}

unittest { assert(false); }

void main() {}


output:
assert in test at line 16
Segmentation fault

This is with dmd 2.031 on linux. Is this a bug, am I doing something wrong? 
Jul 24 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, Jul 24, 2009 at 4:37 PM, Lutger<lutger.blijdestijn gmail.com> wrote=
:
 There is a function setAssertHandler in druntime, but when I try to use i=
t
 it segfaults. I'm not sure how it should be used, this is a complete exam=
ple
 of what I try to do:

 import std.stdio;
 import core.exception;

 void handleAssertion(string file, size_t line, string msg =3D null)
 {
 =A0 =A0writefln("assert in %s at line %s", file, line);
 };

 static this()
 {
 =A0 =A0setAssertHandler( &handleAssertion =A0);
 }

 unittest { assert(false); }

 void main() {}


 output:
 assert in test at line 16
 Segmentation fault

 This is with dmd 2.031 on linux. Is this a bug, am I doing something wron=
g? Hm, it might - and I'm just taking a wild guess here - be that std.stdio hasn't yet been initialized when you do the writefln in your assertion handler. But you really should try using a debugger to get a stacktrace.
Jul 24 2009
parent reply Lutger <lutger.blijdestijn gmail.com> writes:
Jarrett Billingsley wrote:

 On Fri, Jul 24, 2009 at 4:37 PM, Lutger<lutger.blijdestijn gmail.com>
 wrote:
 There is a function setAssertHandler in druntime, but when I try to use
 it it segfaults. I'm not sure how it should be used, this is a complete
 example of what I try to do:

 import std.stdio;
 import core.exception;

 void handleAssertion(string file, size_t line, string msg = null)
 {
 writefln("assert in %s at line %s", file, line);
 };

 static this()
 {
 setAssertHandler( &handleAssertion  );
 }

 unittest { assert(false); }

 void main() {}


 output:
 assert in test at line 16
 Segmentation fault

 This is with dmd 2.031 on linux. Is this a bug, am I doing something
 wrong?
Hm, it might - and I'm just taking a wild guess here - be that std.stdio hasn't yet been initialized when you do the writefln in your assertion handler. But you really should try using a debugger to get a stacktrace.
Ok I tried. Funny thing, with -g enabled it doesn't segfault anymore. Without -g, the trace is not intelligible to me. Segfault also occurs when commenting out writefln. This is useless I assume but anyway, this is what gdb gives me: Backtrace stopped: previous frame inner to this frame (corrupt stack?)
Jul 24 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, Jul 24, 2009 at 5:46 PM, Lutger<lutger.blijdestijn gmail.com> wrote=
:
 Jarrett Billingsley wrote:

 On Fri, Jul 24, 2009 at 4:37 PM, Lutger<lutger.blijdestijn gmail.com>
 wrote:
 There is a function setAssertHandler in druntime, but when I try to use
 it it segfaults. I'm not sure how it should be used, this is a complete
 example of what I try to do:

 import std.stdio;
 import core.exception;

 void handleAssertion(string file, size_t line, string msg =3D null)
 {
 writefln("assert in %s at line %s", file, line);
 };

 static this()
 {
 setAssertHandler( &handleAssertion =A0);
 }

 unittest { assert(false); }

 void main() {}


 output:
 assert in test at line 16
 Segmentation fault

 This is with dmd 2.031 on linux. Is this a bug, am I doing something
 wrong?
Hm, it might - and I'm just taking a wild guess here - be that std.stdio hasn't yet been initialized when you do the writefln in your assertion handler. =A0But you really should try using a debugger to get a stacktrace.
Ok I tried. Funny thing, with -g enabled it doesn't segfault anymore. Without -g, the trace is not intelligible to me. Segfault also occurs whe=
n
 commenting out writefln.

 This is useless I assume but anyway, this is what gdb gives me:





 Backtrace stopped: previous frame inner to this frame (corrupt stack?)
I'm.. utterly at a loss. It seems that exiting the assertion handler by doing anything other than throwing an exception causes the segfault. And like you said, -g makes the problem disappear. I have no idea what's going on.
Jul 24 2009
parent Lutger <lutger.blijdestijn gmail.com> writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3208
Jul 24 2009