[Issue 1648] New: Casting a function literal to void* swallows the function or segfaults

Christopher Wright dhasenan at gmail.com
Thu Nov 8 08:42:03 PST 2007


Jarrett Billingsley wrote:
> <d-bugmail at puremagic.com> wrote in message 
> news:bug-1648-3 at http.d.puremagic.com/issues/...
>> http://d.puremagic.com/issues/show_bug.cgi?id=1648
>>
>>           Summary: Casting a function literal to void* swallows the
>>                    function or segfaults
>>           Product: D
>>           Version: 2.007
>>          Platform: PC
>>        OS/Version: Linux
>>            Status: NEW
>>          Severity: normal
>>          Priority: P2
>>         Component: DMD
>>        AssignedTo: bugzilla at digitalmars.com
>>        ReportedBy: dhasenan at gmail.com
>>
>>
>> When saving a delegate as a void* and only using one delegate from the 
>> same
>> function, attempts to execute the delegate by casting to the original type 
>> do
>> nothing.
>>
>> When saving multiple delegates from the same function, if any of them are 
>> cast
>> to void*, a segmentation fault occurs.
>>
>> Casting is inherently dangerous, I know, but there's no other way to pass 
>> a
>> delegate while ignoring its type.
>>
>>
>> -- 
>>
> 
> delegate.sizeof != (void*).sizeof.
> 
> It's no real surprise that this doesn't work.  Delegates are not simple 
> pointers; they are actually a struct with two members, but you shouldn't 
> really be relying on the underlying implementation in any way.
> 
> What is it that you're trying to do, anyway?  Delegates are strongly typed 
> for a reason -- the compiler can't generate the correct call sequence unless 
> it knows the exact type of the delegate.  There's probably a much better way 
> of doing what you want. 

(delegate*).sizeof == (void*).sizeof
The example casted the address of the delegate to void*.

I tried using Variant.coerce to retrieve a boxed delegate, but that 
failed (delegates aren't numeric types, objects, or strings, and coerce 
only supports those). Now I've found that Variant.peek does support 
delegates, so I'm set.


More information about the Digitalmars-d-bugs mailing list