Monday, February 1, 2010

Properties and Retain

I just found the answer to a problem that has been bedeviling me for a while, I had a crash with a deallocated object. The object was a property and I was POSITIVE that I had the proper memory management. I could workaround it with an extra retain, but I hated that solution.

I finally found an answer on Stack Overflow by Chris Hanson:


I had a property named currentWords that I was setting equal to another value:

@property (nonatomic,retain) NSArray * currentWords;


and using

currentWords=result;


The problem was that invoking a variable this way DOES not invoke the retain or other characteristics of the automatically created setter. The proper way to represent this is:


self. currentWords=result;


I'm sure for someone used to Objective-C this is obvious, but coming from C# and Java this was not an obvious error, and caused me much debugging. I had a workaround with an extra retain, but I don't want to have bogus code present, I want to solve the problem for real.


No comments:

Post a Comment