openfrog
openfrog

Reputation: 40755

How to track at which point a variable gets changed?

I have a BOOL instance variable which -for some reason- some time later always reads NO even if I set it to YES (Yes, I am not so stupid to write or read the wrong variable, trust me). I spent an hour searching my whole project for any possibilities where that variable might be set to NO but can't find it.

Is there a way to trace changes of it's value, i.e. to let the debugger halt at exactly the position where the variable is about to be modified?

Upvotes: 4

Views: 1920

Answers (3)

Serhii Mamontov
Serhii Mamontov

Reputation: 4932

Refactor code to make your BOOL value @property and use KVO to listen when it will be changed. In listener method you can add next line to log out call stack:

NSLog(@"Stack: %@", [NSThread callStackSymbols]);

Upvotes: 1

openfrog
openfrog

Reputation: 40755

Found a solution without hacking the code:

  1. Launch debugger with breakpoints enabled

  2. Set a breakpoint at some point where the variable in question is in scope

  3. Run. When it halts, inspect the debugger and find that variable.

  4. Right-click on it and choose "Watch Variable".

Upvotes: 11

Mr. Berna
Mr. Berna

Reputation: 10655

Can you structure your code so that this BOOL instance variable is a property? You could then write your own setter function for this property and then set a breakpoint in that setter function. The stack trace will tell you what called the setter method. Or you could use Key-Value Observing to watch for changes in that property at runtime in your app, but that adds more complexity.

Upvotes: 3

Related Questions