iOS 5 Best Practice Release/retain?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
For iOS 5 era development, the best practice was already shifting toward ARC for new code, but many projects still used manual retain-release. If you are working in manual memory management, the core rule is simple: if you own an object because you created it, copied it, or retained it, you are responsible for releasing it later.
The Ownership Rule
Under manual retain-release, you own an object when you receive it through methods such as:
- '
alloc' - '
new' - '
copy' - '
mutableCopy' - '
retain'
Example:
The release call is required because the code created an owned object.
Objects You Do Not Own
If a method returns an autoreleased object, you usually should not release it yourself.
Here, you did not create or retain the string, so you should not call release on it.
Properties Matter
Property attributes express ownership intent. In manual memory management, a retained property is commonly declared like this:
And in dealloc:
This keeps the property and deallocation logic aligned.
Avoid Over-Retain and Over-Release
Two classic mistakes are:
- Releasing an object you do not own
- Forgetting to release an object you do own
The first causes crashes. The second causes leaks.
That is why older Objective-C codebases often follow a strict ownership checklist rather than relying on memory-management intuition.
ARC Versus Manual Memory Management
By the iOS 5 timeframe, ARC was already the preferred choice for new code when the toolchain allowed it. ARC removes most direct retain, release, and autorelease calls by letting the compiler insert them automatically.
So the best-practice answer is:
- New code: prefer ARC
- Legacy non-ARC code: follow ownership rules rigorously and consistently
Understanding retain-release still matters because many older APIs, code samples, and legacy projects reflect those rules directly.
It also matters when reading old Objective-C explanations, because many "best practice" recommendations from that period were really ownership-rule reminders written for non-ARC codebases.
Example of a Balanced Pattern
This is correct because:
- '
itemswas created withalloc, so it is owned locally' - Assigning to a retained property transfers ownership to the property
- The local ownership is then released
That pattern, create then hand off then release local ownership, is one of the most common manual-memory-management flows in older iOS code.
Common Pitfalls
- Releasing autoreleased objects that you never owned.
- Forgetting to release retained instance variables in
dealloc. - Mixing ARC and manual memory management rules mentally and applying the wrong one.
- Calling
releasetoo early while another part of the code still assumes ownership.
Summary
- In manual memory management, ownership determines whether you must release.
- If you
alloc,copy, orretain, you must laterreleaseorautorelease. - Do not release objects you did not own.
- Use ARC for new code when possible.
- For legacy iOS 5 era code, consistency with the ownership rules is the real best practice.
That consistency is what keeps old Objective-C code stable. It is also what makes old bugs easier to reason about.
Related reading
- iOS 6 apps - how to deal with iPhone 5 screen size?
- ios app maximum memory budget
- ios crash EXC_BAD_ACCESS KERN_INVALID_ADDRESS
- iOS Development How can I induce low memory warnings on device?
- iOS 6 How do I restrict some views to portrait and allow others to rotate?
- iOS 7.0 No code signing identities found
- iOS start Background Thread
- iOS what''s the fastest, most performant way to make a screenshot programmatically?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.