Top Posts
Introducing Adventure Modes: The Right Map for Every...
Trip Report: A Rider’s Guide to Bikepacking in...
How to Hike a Colorado 14er: A Step-by-Step...
Trip Report: A Rider’s Guide to link Boulder...
Trip Report: A Rider’s Guide to the South...
Battling the Wind: Setting Your Shocks to Firm...
The Hike Map That Broke Me: How Gaia...
A Fond Farewell to National Geographic Maps —...
Introducing the Gaia Hike Map: Plan Your Hike,...
Download the app and get a free 14-day...
Gaia GPS
  • Explore The Map
  • Get the App
  • Upgrade Today
  • Explore The Map Catalog
  • New Features
  • Activities
    • Backcountry Skiing
    • Boating
    • Emergency Response
    • Fishing
    • Offroading
  • Adventures
    • User Stories
  • Help

(Technical Post) How Memory Works on iOS

by Staff Reports May 26, 2011
written by Staff Reports
1.4K

When you are programming iPhone/iPad apps, the thing that tricks everyone up the most is memory management. If you are a beginner, you are likely to just not know what is going on. If you are experienced, then you find yourself battling obscure memory bugs in errant threads.

It has taken me some time to master, but I have gotten the hang of it, and it boils down to knowing just a few things. Here’s what I know about memory management on iPhone/iPad. Hopefully, this will help someone out, and if I have any errors in my thinking, someone will let me know.

Memory in Objective C

  1. Every time you type “retain” you also need to type “release.”
  2. When you type “alloc” there is an implied retain.
  3. When you type self.foo = bar, there is an implied retain. If you set a class variable without the “self,” there is no implied retain. EDIT: Someone on Hacker News corrected me – there is only an implied retain if you declare the property as retained in the header, with @property(retain).
  4. If you use a convenience method to make an object, then there is an implied “autorelease” and you don’t need a release.

Correct Examples

Each of these examples is correct. The object is properly instantiated, and then later released.

Ex. 1

NSString *foo = [[NSString alloc]init];


...


[foo release];




Ex. 2


NSString *foo = [[[NSString alloc]init]autorelease];


...




Ex. 3


self.foo = [NSString stringWithFormat:@"bar"];


...


self.foo = nil;




Incorrect Examples

All of these examples are wrong.

This example will crash, because foo is over-released:

NSString *foo = [[[NSString alloc]init]autorelease];


...


[foo release];

This will also crash and is over-released:


NSString *foo = [NSString stringWithFormat:@"bar"];


...


[foo release];




A Note on @synthesize

If you declare a class variable as a property in the header, and then synthesize it in the implementation, that will auto-generate getters and setters.

That means when you type:

self.foo = @"bar";

The following is actually happening, and you could even override this method:


- (void) setFoo:(NSString*) bar {


  if(bar == foo) {


    return;


  }


  [foo release];


  foo = [bar retain];


}

EDIT: Corrected the above function based on comment on HN… shows what I know!

So, whenever you call self.foo = bar, there is both a release and then a retain.

Typing self.foo = nil releases and nils the variables.

Just typing foo = nil is a memory leak.

previous post
Gaia Devs Married
next post
Gaia GPS Ratings are Groovy

You may also like

Introducing Adventure Modes: The Right Map for Every...

August 13, 2026

Trip Report: A Rider’s Guide to Bikepacking in...

November 7, 2025

How to Hike a Colorado 14er: A Step-by-Step...

October 24, 2025

Trip Report: A Rider’s Guide to link Boulder...

September 26, 2025

Trip Report: A Rider’s Guide to the South...

August 27, 2025
Staff Reports

Categories

  • Adventures
  • Android
  • App Comparisons
  • App Updates
  • Backcountry Skiing
  • Boating
  • Company News
  • Emergency Response
  • Featured
  • Fishing
  • Gaia GPS
  • Gaia GPS Offroad Podcast
  • GaiaCloud
  • Hikes
  • How-To
  • Hunting
  • iOS
  • New Features
  • New Maps
  • Newsletter
  • Offroading
  • Out and Back Podcast
  • Overlanding
  • User Profiles

Recent Posts

  • Introducing Adventure Modes: The Right Map for Every Adventure in One Tap
  • Trip Report: A Rider’s Guide to Bikepacking in Bears Ears National Monument
  • How to Hike a Colorado 14er: A Step-by-Step Guide with Gaia GPS
  • Trip Report: A Rider’s Guide to link Boulder and Steamboat Springs
    • Facebook
    • Twitter
    • Instagram
    • Youtube
    • Rss
    • Explore The Map
    • Get the App
    • Upgrade Today
    • Explore The Map Catalog
    • New Features
    • Activities
      • Backcountry Skiing
      • Boating
      • Emergency Response
      • Fishing
      • Offroading
    • Adventures
      • User Stories
    • Help

    @2024 - All Right Reserved. Gaia GPS


    Back To Top
    Gaia GPS
    • Explore The Map
    • Get the App
    • Upgrade Today
    • Explore The Map Catalog
    • New Features
    • Activities
      • Backcountry Skiing
      • Boating
      • Emergency Response
      • Fishing
      • Offroading
    • Adventures
      • User Stories
    • Help