Problems with function as parameter

user1234 user1234 at 12.hu
Fri Sep 22 07:40:59 UTC 2017


On Friday, 22 September 2017 at 04:32:08 UTC, Josh wrote:
> As an aside, in that doc it says "The .funcptr property of a 
> delegate will return the function pointer value as a function 
> type". So I also tried 
> Mix_ChannelFinished((&channelDone).funcptr); and this compiled, 
> but caused a segfault when
> the callback ran. What would have caused this? Is it because 
> it's a C function?

No it's because in this function are used variables that are 
specific to the class instance (the this). If there weren't this 
would work, even if it's not a good idea to do that:


struct Foo
{
     int a;
     void needThisYeahReally(){a = 0;}
     void needThisButWorkWithout(){}
}

void main()
{
     Foo foo;
     {
         auto dg = &foo.needThisButWorkWithout;
         dg.funcptr();
     }
     {
         auto dg = &foo.needThisYeahReally;
         // dg.funcptr(); // segfault because of access to this.a
     }
}


Using a pointer to a static member function was the right thing 
to do.


More information about the Digitalmars-d-learn mailing list