pithhelmet
pithhelmet

Reputation: 2292

Best memory management routine

I am writing an iPhone application, and now it is time to start cleaning up memory.

By a better programmer than myself, i was told that each time I perform a alloc, that I should dealloc the memory at the end of the module.

Is this statement that each time there is a alloc, that there should be a removal in the dealloc section of the .m file??

thanks tony

Upvotes: 1

Views: 153

Answers (2)

Abizern
Abizern

Reputation: 150765

Rather than just listen to what your colleague has told you - Have a read of the Memory Management Guide.

Memory management isn't something you do at the end as 'cleaning up' it's something you need to think about when programming. Don't just rely on what somebody tells you is a rule of thumb. Read the documents, understand them, and then use the rules of thumb to help you remember what they are.

For example - When you create a local variable in a method with alloc, you can't wait until the dealloc to release it, because by then that variable has gone out of scope, you don't have an object to call release on' and you have a leak. So that rule of thumb isn't any good.

Upvotes: 3

Dancreek
Dancreek

Reputation: 9544

You might want to read up on the memory management guides on the Apple developer site. Basically you need to have a release or autorelease for every new, copy, or alloc you use. But the release ideally should be in the function that called new, copy, or alloc, not in your dealloc function. dealloc should only be used for releasing objects that were retained in the @property section of your header file.

Upvotes: 1

Related Questions