Anatoley Kovalev
Anatoley Kovalev

Reputation: 59

I need some clarification about whether I should (could?) deallocate view-related UI elements or not

So I do understand, that I should release the objects that I own. (alloc/copy/retain stuff)

And I also do understand(kinda?), that if I had @property with retain in some of my class, or some instance variable with alloc/init, then I should write a custom dealloc to make sure those ones gets deallocated with that class.

What I do not understand is should I manually release the cell objects for example? (since I am creating it in cellForRowAtIndexPath with alloc/init? Yea, I know, that tableView should kill those cells when it gets deallocated, but do I want to make sure?

Also, could I manually release UILabel, UITextField and so on? I do know that those are gets killed when you deallocate the view, but again, still, do I want to make sure?

UPDATE: Yea, thanks for the tip about reference counting, but the project exact purpose is to do some simple stuff, using manual memory management. It is funny how most of the tutors nowadays skips this part entirely so the novice like myself have to figure all that stuff by themselves.

Upvotes: 1

Views: 42

Answers (2)

Islam
Islam

Reputation: 3733

Unless you have a strong/retain pointer to a UI element they will be automatically dealloced once they go out of the screen.

Regarding the cells - you should never dealloc them because they're intended to be reused. If you dealloc and re-init them you'll get a REALLY bad performance while scrolling.

Also, as Glorfindel said, consider using ARC (if you're not using already) and your life will be much easier.

Upvotes: 1

Glorfindel
Glorfindel

Reputation: 22651

Objective C supports Automatic Reference Counting which does almost all memory management for you. I suggest you migrate your old code to ARC; this is quite easy (Xcode has refactoring support for this) and it will save you a lot of hassle.

Upvotes: 2

Related Questions