What's wrong with this code?

Chris via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 19 10:21:22 PDT 2015


On Saturday, 19 September 2015 at 02:45:54 UTC, Adam D. Ruppe 
wrote:
> On Saturday, 19 September 2015 at 02:30:39 UTC, Chris wrote:
>> 			bmove.addOnClicked (delegate void (Button aux) {
>
> What's the context of this call? If it is inside a struct and 
> you are accessing local variables you can get in trouble 
> because the context pointer may not be correct. You are using 
> `vec` there, if `glw` is a struct/class member too, then the 
> compiler can sometimes be a bit stupid about bringing in the 
> correct `this` reference to the delegate.

Hi thanks for the response.

The context is inside a try block in the program's main() 
function. Glw is indeed a class but it doesn't look like it quite 
configures the problem you describe.

> I think #3 will work for you here. Right above that loop, try 
> something like
> 
> auto glw = this.glw;

There's no "this", it's the main() function. I stuck a "auto glw2 
= glw" in there but it didn't help.

I found more funny stuff. If I move the code around and change 
the order of that snippet to before the other one I posted 
earlier, the first line (if (!glw...) now works *despite* the 
debugger saying the variable glw is "not found". So now as long 
as the condition evaluates to true it will work as expected. 
However when the condition is made to eval to false then the 
*next* line of code will trigger the exception as I first 
encountered.

So I'm going to go ahead and say I found a compiler bug.


More information about the Digitalmars-d-learn mailing list