A nasty trap - Class Properties Changing Value Whilst A Program Was Paused !!

  • Thread starter Thread starter pigeonrandle
  • Start date Start date
P

pigeonrandle

Hi,
I thought i should give a better explanation to my second 'bug' in my
previous post (Bugs in VS2003 SP1) because it's taken me ages to figure
out what was going on.

Basically, one of my project classes 'Tokenizer' had a property
'_Token'.
When i hovered my mouse over a line of code with 'Tokenizer._Token' in
it, i saw the value i expected. If i entered 'Tokenizer._Token' into
the watch window i saw the value i expected.

BUT! When i entered 'Tokenizer' into the same watch window and expanded
the treeview to view the '_Token' property it had mysteriously changed.
Hmmmmm.

Unfortunately for me, one of my other properties '_CommandType' resets
the tokenizer to the start of the string and gets the first token ...
which i only ever called once in my code at the beginning of the whole
tokenizing procedure, so my code never experienced any problems.

HOWEVER, when i enter 'Tokenizer' into the watch window, it obviously
gets '_CommandType' for the treeview, thus reseting the tokenizer to
the beginning and displaing the 'wrong' value in the watch window. Oh
how i now laugh through my tears.

I really hope this helps some poor, distraught person.

Please feel free to call me names. I would like to start the list with
'Moron'.

Cheers,
James Randle.
 
Hi James,

You're not a moron - at least not because of this mistake :)

I've seen it happen many times. Developers will find a bug in their code
and try to debug it by listing properties in a watch window, but then
completely forget about them. Then, they report all sorts of weird behavior
that they think is related to the original bug. One indication of this
might be when they say, "I can't reproduce the problem in the debugger".
Right, because of the observer effect - as soon as you attach to the process
you're changing it.

This is something that all developers should be aware of when using the
debugger. One way this issue can be alleviated is to make sure that
properties don't change internal state or call methods in their get
accessors except for when lazy initialization is desirable for the backing
field.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top