0

I have a view controller with a BOOL property (not a pointer) which I assumed would be NO or just garbage seeing as though it doesn't get assigned in viewDidLoad. The BOOL gets assigned when it is needed. When the view controller gets deallocated (I'm using arc) and later recreated, if the previous instances value of that BOOL was YES (before the VC was deallocated) then the initial value of the new instances BOOL variable will also be YES. Finding this very strange.

  1. Property declared (nonatomic, assign)
  2. It's not set in viewDidLoad, set elsewhere in an action method
  3. Not changed in dealloc

I know this is strange, in the meantime I've set the appropriate default values in viewDidLoad.

UPDATE

Bearing in mind this was happening with the BOOL as an iVar (I changed it to a property) and the comparison was made before the BOOL was assigned. I'm putting the strange behaviour down to that.

2 Answers2

0

With garbage you have a much bigger chance of getting YES than NO, because more values are !=0 than are ==0 :)

However, AFAIK instance variables of type BOOL will be initialized to NO (because all ivars are set to 0), and even though I did not look it up, I would assume this also holds true for auto-synthesized property backing ivars. That would speak for it being NO every time.

Do you observe that it always reflects the old state, i. e. both YES and NO being preserved after dealloc? My guess is that either the above assumption is not correct OR the instances are not really dealloc'ed. Have you checked if the instance addresses are maybe identical?

Daniel Schneller
  • 13,728
  • 5
  • 43
  • 72
  • I've checked the addresses and they're not identical. If the previous instance was deallocated with the BOOL property value being YES then the new instance will initially hold a value of YES and vice versa. – BodaciousPie Mar 19 '13 at 15:24
  • I think you will need to add some code to your question. Just from the description I have no idea what that could be. – Daniel Schneller Mar 19 '13 at 15:48
0

Turns out they were initially global variables which preserved them between instances.