21

The problem is that when I place a breakpoint and debug/run, the breakpoint moves by itself.

Before/whilst coding:

enter image description here

After clicking run/breakpoint hit:

enter image description here

Breakpoints typically work ok for me, but it seems that they sometimes randomly play up. I first had this ages ago with a VB project, and in the end, I bypassed the problem by removing the breakpoint and adding it somewhere else where it was still useful.

Whilst I could probably do the same again, and this is only the second time it has happened (that I remember), I don't really want to have to and would like to know what is actually wrong.

I have read through many similar questions here, but I cannot see an exact match and the answers do not help. I have tried - building, rebuilding, closing/reopening and cleaning.

I only provided a picture of the bit of code where it occurs, if you need anything else, please let me know.

Wil
  • 10,234
  • 12
  • 54
  • 81

5 Answers5

14

It's because the debugger isn't able to break at that point. For example, the debugger can't break on auto-implemented properties, or at the header of a method; instead, it breaks at the first line of the method.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • I was really hoping it would break when there was a change/id was set. IF I extend the set method, would I be able to set a breakpoint there? – Wil Feb 15 '11 at 23:09
  • 2
    @Wil You could always set a conditional breakpoint that will break when the value of `Result` changes (right-click the breakpoint and pick "Condition...") – Dan J Feb 15 '11 at 23:32
  • I accepted, but I don't suppose you can explain a little more. Why is it stopping at the next method which is unrelated? / and are conditional breakpoints the best solution? I tried the solution above, but the breakpoint still moves / does not occur at that point. – Wil Feb 16 '11 at 02:26
  • @Wil: It's just putting the breakpoint at the next valid place; it doesn't care what method it's in. Also, I don't think that making the breakpoint conditional changes anything; it still can't stop at an auto-implemented property. – user541686 Feb 16 '11 at 02:39
  • 1
    To set a breakpoint for an auto-implemented property see my answer here: http://stackoverflow.com/questions/2682613/cant-set-breakpoints-on-an-auto-property-setter-why/6713920#6713920 – Matt Smith Jul 15 '11 at 22:44
12

Also check to see if you had set the breakpoints in one mode (Debug) but are now in a different mode (like Release). This caused me some momentary angst.

Mike Burger
  • 321
  • 3
  • 4
3

I got this problem after Windows had inexplicably added 6 months to the current system date (and 1 hour to the time). I didn't correct this right away, so builds made before the correction looked more recent to Visual. This lead to running the wrong (older) build when debugging.

Since I couldn't find a "Clean Solution/Project" option, I had to manually delete all .pdb files in the solution. The problem was instantly fixed. I just hope there aren't any more files I might need to delete (I'm new to Visual and don't know much about how it works behind the scenes).

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
0

You can get this if the breakpoint is AFTER a return statement. Visual Studio will bounce the breakpoint to the closing brace (or "End Function" for VB.NET) because return is jumping out of the function.

amonroejj
  • 573
  • 4
  • 16
0

for Visual Studio Code users, it might be related to TS/NODE :

T.Todua
  • 53,146
  • 19
  • 236
  • 237