SRI
SRI

Reputation: 1532

Releasing properties

I have two NSStrings and those are also having the property, synthesize. Is there any need to release those objects?

Upvotes: 0

Views: 128

Answers (3)

Himadri Choudhury
Himadri Choudhury

Reputation: 10353

Whether you should call release actually depends.

If you called alloc+init, and then assigned to the property, then you do have to release. The reason is that alloc+init increases the reference count by 1, so you have to have a corresponding release to return the reference count to 0. The release should be in the dealloc method of the class.

However, it's possible to use those properties without calling alloc+init too. Then generally you don't have to call release. For example if you use self.property = [NSString stringWithFormat:@"..."], then there is no need to call release, actually calling release would be wrong.

Furthermore, you may also decide to autorelease objects, which tells the autorelease pool to automatically release objects at a later time. This is another case where you don't have to manually release the object.

Upvotes: 0

Matthias Bauch
Matthias Bauch

Reputation: 90117

If the property is either copy or retain you have to release the ivar. If the value is just assigned you must not release.

Upvotes: 3

Swastik
Swastik

Reputation: 2425

Yes! Anything which is pointer type which is made property & synthesise should be released

Upvotes: 1

Related Questions