digitalmars.D.bugs - [Issue 851] New: strange bug when a delegate access a member variable.
- d-bugmail puremagic.com (25/25) Jan 16 2007 http://d.puremagic.com/issues/show_bug.cgi?id=851
- d-bugmail puremagic.com (5/5) Jan 17 2007 http://d.puremagic.com/issues/show_bug.cgi?id=851
- d-bugmail puremagic.com (24/24) Feb 12 2007 http://d.puremagic.com/issues/show_bug.cgi?id=851
http://d.puremagic.com/issues/show_bug.cgi?id=851 Summary: strange bug when a delegate access a member variable. Product: D Version: 1.00 Platform: PC URL: http://dsource.org/projects/qonkd/browser/trunk/bug OS/Version: Linux Status: NEW Severity: blocker Priority: P1 Component: DMD AssignedTo: bugzilla digitalmars.com ReportedBy: rodolfo.borges gmail.com 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 --
Jan 16 2007
http://d.puremagic.com/issues/show_bug.cgi?id=851 ------- Comment #1 from rodolfo.borges gmail.com 2007-01-17 19:49 ------- 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. --
Jan 17 2007
http://d.puremagic.com/issues/show_bug.cgi?id=851 lio lunesu.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |INVALID ------- Comment #2 from lio lunesu.com 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. --
Feb 12 2007