tags:

views:

58

answers:

2

do you need to release something very simple this?

NSString *a = @"Hello";

//[a release];  ?

i come from a java/c# world, and am confused about when things should be released/retained...

+2  A: 

No. You only need to release objects you init/alloc yourself or your instance variables in your class dealloc method.

Jason McCreary
what about synthesized IB properties?
Yaso
Depends on if their an object type, e.g. NSString yes, NSInteger no. But as I said above, these would go in your `dealloc` method.
Jason McCreary
You need to do release all IB properties in your dealloc, and all properties that your object has taken a retain on, it has nothing to do with object type. The only reason you don't release an NSInteger is that it is not an object at all, it is a scalar. On the other hand you would release an NSNumber.
Louis Gerbarg
+1 but not entirely correct. You `release` if you created the string via a call to a method that contains `new`, `alloc`, `retain`, or `copy`. `alloc/init` is not the only case.
Dave DeLong
+1  A: 

No, you do not need to release a constant NSString, though it doesn't cause any problems if you do. Constant strings are special case of the memory management system. Since their content is known at compile time, it is statically defined in the application binary itself, so it never has to be allocated or freed at runtime. For that reason, its retain and release methods are noops.

This is only true for constant NSStrings (strings that start with @), and their toll free bridged cousin, constant CFStrings (defined using the CFSTR() macro).

Louis Gerbarg