Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java/Eclipse, why does setting a breakpoint in my code alter the effect of that statement?

I'm writing an Android app in Eclipse, and displaying some text on screen using a TextView. A different thread sometimes changes the text of this view, with the following code:

runOnUiThread(new Runnable() {

    @Override
    public void run() {
        view.setText(newLyric);
    }
});

I wanted to debug the setText line to check that the correct String was being passed in (as for some text, it was not being word-wrapped to fit the screen, and for other text it was, but that's a different issue). When I put a breakpoint on that line, the text is always word-wrapped. And when I disable/remove the breakpoint, the text that usually doesn't get word-wrapped (it's deterministic) is not word-wrapped.

This doesn't make much sense to me. I've seen this question, and I understand the reasoning behind that, but I can't see why that would be the issue here. Can anyone shed some light on this? I'm testing on a device, if that makes any difference.


Edit: It turns out it was a race condition, now I just have to work out how to fix it... Obviously I could only pick one 'correct' answer, but thank you to all of you for the helpful comments :)

like image 301
epochengine Avatar asked Feb 24 '23 04:02

epochengine


2 Answers

Things like this are sometimes/often a sign of a race condition.

Putting a breakpoint eliminates the race (so a "rogue" thread that was overriding the value by running later is now finished by the time your breakpoint is caught, as an example).

Or it's a clear case of Heisenbug :)

like image 150
ZenMaster Avatar answered May 02 '23 08:05

ZenMaster


Try setting setting the property Halt VM for your breakpoint. The default behaviour is to just halt the running thread. If you are getting "contamination" from other threads this should stop that from happening.

like image 22
Robin Avatar answered May 02 '23 08:05

Robin