Bug 851 – strange bug when a delegate access a member variable.

Status
RESOLVED
Resolution
INVALID
Severity
blocker
Priority
P1
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2007-01-16T20:52:00Z
Last change time
2014-02-15T13:12:50Z
Assigned to
bugzilla
Creator
rodolfo.borges

Comments

Comment #0 by rodolfo.borges — 2007-01-16T20:52:39Z
I was implementing a menu system for my game, when I found this bug. The menu item object is created with a delegate for the action to be taken. When I pass a member function as the delegate, and it tries to access a member variable, it crashes. The funny thing is (that helped me think it can be a dmd bug), if the calling part is inside a loop or function, it crashes, but if it's inside the main, it works ok. Check the version(crash) part in main.d to see what I'm talking about. The menu.d file is unchanged, but I tried and made main.d the minimum possible to reproduce the bug. I hope it's clear enough. http://dsource.org/projects/qonkd/browser/trunk/bug
Comment #1 by rodolfo.borges — 2007-01-17T19:49:59Z
I'd just like to add that this bug has been around since at least dmd v0.167. Sorry I didn't reported it before.
Comment #2 by lio+bugzilla — 2007-02-12T03:45:58Z
This is not a bug. This issue gets posted on the newsgroups at least once per month, and I'd suggest you search the groups for more detailed information. It's not a bug: nested functions are only valid inside the function where they are declared. This is because those function expect the stack to be exactly the way it was in that function. In your example, do_start is only valid inside the constructor. You can create delegates to nested function, but these delegates are not valid outside the function. When these delegates are invoked, the stack might be different, which means the this-pointer cannot be found. As I said: the stack might be different. This is why it might work if the stack is left intact. If you do something that changes the stack (like an extra function call, or declare some variables) the stack will have changed and the delegates will no longer work. To solve your code, move the do_start to the class. Then, it will no longer depend on the stack, but it gets the "this" pointer directly (instead of a stack pointer) and you can safely create and return delegates to it.
Comment #3 by rodolfo.borges — 2007-02-12T07:12:55Z
Thanks for the info. Is it possible to have a better error handling behavior in that case, instead of a mysterious segfault? On 2/12/07, [email protected] <[email protected]> wrote: > http://d.puremagic.com/issues/show_bug.cgi?id=851 > > > [email protected] changed: > > What |Removed |Added > ---------------------------------------------------------------------------- > Status|NEW |RESOLVED > Resolution| |INVALID > > > > > ------- Comment #2 from [email protected] 2007-02-12 03:45 ------- > This is not a bug. > > This issue gets posted on the newsgroups at least once per month, and I'd > suggest you search the groups for more detailed information. It's not a bug: > nested functions are only valid inside the function where they are declared. > This is because those function expect the stack to be exactly the way it was in > that function. In your example, do_start is only valid inside the constructor. > You can create delegates to nested function, but these delegates are not valid > outside the function. When these delegates are invoked, the stack might be > different, which means the this-pointer cannot be found. > > As I said: the stack might be different. This is why it might work if the stack > is left intact. If you do something that changes the stack (like an extra > function call, or declare some variables) the stack will have changed and the > delegates will no longer work. > > To solve your code, move the do_start to the class. Then, it will no longer > depend on the stack, but it gets the "this" pointer directly (instead of a > stack pointer) and you can safely create and return delegates to it. > > > -- > Configure bugmail: http://d.puremagic.com/issues/userprefs.cgi?tab=email > ------- You are receiving this mail because: ------- > You reported the bug, or are watching the reporter. >