Bug 1648 – Casting a function literal to void* swallows the function or segfaults

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2007-11-08T08:04:00Z
Last change time
2015-06-09T01:14:21Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
dhasenan

Attachments

IDFilenameSummaryContent-TypeSize
204mone.da test case that shows the problemtext/plain820

Comments

Comment #0 by dhasenan — 2007-11-08T08:04:09Z
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.
Comment #1 by dhasenan — 2007-11-08T08:04:52Z
Created attachment 204 a test case that shows the problem
Comment #2 by dhasenan — 2007-11-08T10:41:25Z
Jarrett Billingsley wrote: > <[email protected]> wrote in message > news:[email protected]/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: [email protected] >> ReportedBy: [email protected] >> >> >> 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.
Comment #3 by dhasenan — 2007-11-08T17:14:58Z
As Jarrett Billingsley pointed out, the error was in my code: taking the address of a local variable.