0

I am saving username/password info to keychain upon a user logging in. Later if I need the username I get the username from the keychain. I noticed this morning when navigating my app that my keychain property has apparently been deallocated from memory even though the app never was deallocated (never got kicked out to initial view controller upon re-opening app).

I am using this ARCified KeychainItemWrapper, and I think the issue might be the way that I declared the property attribute:

@property (nonatomic, retain) KeychainItemWrapper *keychainItem;

I now know that I should be using strong and not retain, but is that the reason that the property was deallocated before my app?

Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • Most likely, the object *containing* your property got deallocated. – Hot Licks Jan 13 '15 at 22:38
  • Just wanted to say that 'strong' is same as 'retain', so this is not the reason. But anyway you should use strong. And as @HotLicks said, check if the viewController holding this property is getting deallocated as well, probably is. – oren Jan 13 '15 at 23:07
  • My question was actually a little misguided. I was trying to guess what was happening before I got home to debug. I had actually accidentally created multiple keychainItems and not all of them had username key set. Later when I tried to get username, it was from a keychainItem object that had never had username key set. – Adam Johns Jan 14 '15 at 14:50

1 Answers1

1

A. It is not important which the class you are using. The important thing is that your property is deallocated. This happen when there are no anymore objects that have the ownership of the object represented from that property. This means that the object where you declared that property is been deallocated (i.e. a view controller dismissed or popped out);

B. retain and strong are exactly the same, but being retain and release no longer called with ARC is a good practice use strong.

Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41