Distinguishing a pointer to a function and a closure

berni44 dlang at d-ecke.de
Mon Nov 11 13:10:44 UTC 2019


While debugging phobos I came across some stuff I don't 
understand. A small example:

void foo(void* p)
{
     Object o = cast(Object) p;
     ClassInfo oc = typeid(o);
}

class Bar
{
     void some_func(int i) {}

     void do_something(void delegate(int) d)
     {
         // is it possible to check here, if d.ptr can be passed 
to foo?
         foo(d.ptr);
     }
}

void main()
{
     auto b = new Bar();

     b.do_something(&b.some_func); // OK
     b.do_something(i => b.some_func(i)); // crashes
}

While the first call to do_something succeeds, the second one 
crashes. That's probably, because a closure is not an 'Object' 
and the cast in foo should never happen (a normal delegate seems 
to be an Object somehow, though). Is there any chance to notice 
this in "do_something" and emergency-exit there (e.g. with some 
assert call)?

Background of the question: Issue 9603 [1] reports some crashes 
when using std.signals. I meanwhile found out, that the crash 
happens inside "_d_toObject", which is a function in the runtime 
[2]. The documentation tells, that this function crashes if the 
pointer does not point to a class, an interface or is the null 
pointer. It would be nice to avoid the crash by some check before 
the call to _d_toObject.

[1] https://issues.dlang.org/show_bug.cgi?id=9603
[2] https://dlang.org/library/rt/cast_/_d_to_object.html


More information about the Digitalmars-d-learn mailing list