Gaia GPS
  • 7-Day Free Trial Gaia GPS Premium
  • Out and Back Podcast
    • Gaia GPS

      Everything You Need to Know About Satellite Communicators

      December 1, 2022

      Adventures

      How to Avoid Another Deadly Avalanche Season

      January 21, 2022

      Gaia GPS

      The Best Advice We Received in 2021

      December 26, 2021

      Gaia GPS

      Avalanche Safety with Snow Science Expert Bruce Tremper

      November 30, 2021

      Gaia GPS

      Planning a Thru-Hike Next Year? Here’s What You…

      October 21, 2021

      Gaia GPS

      A Definitive Guide to the Best Camp Coffee

      September 30, 2021

      Gaia GPS

      Owls and UFOs with Ultralight Expert Mike Clelland

      September 9, 2021

  • Offroad Podcast
    • Gaia GPS

      Cook Marco Hernandez’s Mouthwatering Camp Meals

      January 25, 2023

      Gaia GPS

      Monique Song: How a City Girl Became the…

      April 7, 2022

      Gaia GPS

      TrailRecon Explains How to Become an Overlander Overnight

      March 9, 2022

      Adventures

      How to Stay Married on a Long-Distance Adventure

      February 9, 2022

      Adventures

      How to Go From ‘Desk to Glory’ in…

      January 12, 2022

      Gaia GPS

      The Best Advice We Received in 2021

      December 26, 2021

      Gaia GPS

      Gaia GPS’s Best New Overlanding Maps and Features…

      December 23, 2021

  • Adventures
    • User Stories
  • Activities
    • Backcountry Skiing
    • Boating
    • Emergency Response
    • Fishing
    • Offroading
  • New Features
    • Gaia GPS

      Our Favorite New Maps and Features

      December 23, 2022

      Gaia GPS

      OpenSnow Weather Now Available at-a-Tap on the Map

      November 9, 2022

      Backcountry Skiing

      Find the Deepest, Lightest Powder with the Snow…

      November 8, 2022

      Gaia GPS

      New Edit Tools: Creating Your Own Routes Just…

      September 1, 2022

      Gaia GPS

      Top 10 Ways to Use Waypoints

      August 30, 2022

      Gaia GPS

      Meet Map Packs: A Totally New Way to…

      May 9, 2022

      Gaia GPS

      Discover & Reserve Campsites Right From Gaia GPS

      February 22, 2022

  • New Maps
    • Backcountry Skiing

      New! Find Backcountry Skiing in Gaia Winter Map

      January 11, 2023

      Gaia GPS

      Our Favorite New Maps and Features

      December 23, 2022

      Gaia GPS

      Spy Avalanche Terrain with Higher Res Slope Angle…

      December 14, 2022

      Backcountry Skiing

      Find the Deepest, Lightest Powder with the Snow…

      November 8, 2022

      Gaia GPS

      Activate Snow Mode with Gaia Winter

      October 26, 2022

      Adventures

      Take a Trip to Baja California with Nat…

      September 13, 2022

      Gaia GPS

      Gaia Overland: One Map to Rule Them All

      May 24, 2022

  • Home
Gaia GPS
  • 7-Day Free Trial Gaia GPS Premium
  • Out and Back Podcast
    • Gaia GPS

      Everything You Need to Know About Satellite Communicators

      December 1, 2022

      Adventures

      How to Avoid Another Deadly Avalanche Season

      January 21, 2022

      Gaia GPS

      The Best Advice We Received in 2021

      December 26, 2021

      Gaia GPS

      Avalanche Safety with Snow Science Expert Bruce Tremper

      November 30, 2021

      Gaia GPS

      Planning a Thru-Hike Next Year? Here’s What You…

      October 21, 2021

      Gaia GPS

      A Definitive Guide to the Best Camp Coffee

      September 30, 2021

      Gaia GPS

      Owls and UFOs with Ultralight Expert Mike Clelland

      September 9, 2021

  • Offroad Podcast
    • Gaia GPS

      Cook Marco Hernandez’s Mouthwatering Camp Meals

      January 25, 2023

      Gaia GPS

      Monique Song: How a City Girl Became the…

      April 7, 2022

      Gaia GPS

      TrailRecon Explains How to Become an Overlander Overnight

      March 9, 2022

      Adventures

      How to Stay Married on a Long-Distance Adventure

      February 9, 2022

      Adventures

      How to Go From ‘Desk to Glory’ in…

      January 12, 2022

      Gaia GPS

      The Best Advice We Received in 2021

      December 26, 2021

      Gaia GPS

      Gaia GPS’s Best New Overlanding Maps and Features…

      December 23, 2021

  • Adventures
    • User Stories
  • Activities
    • Backcountry Skiing
    • Boating
    • Emergency Response
    • Fishing
    • Offroading
  • New Features
    • Gaia GPS

      Our Favorite New Maps and Features

      December 23, 2022

      Gaia GPS

      OpenSnow Weather Now Available at-a-Tap on the Map

      November 9, 2022

      Backcountry Skiing

      Find the Deepest, Lightest Powder with the Snow…

      November 8, 2022

      Gaia GPS

      New Edit Tools: Creating Your Own Routes Just…

      September 1, 2022

      Gaia GPS

      Top 10 Ways to Use Waypoints

      August 30, 2022

      Gaia GPS

      Meet Map Packs: A Totally New Way to…

      May 9, 2022

      Gaia GPS

      Discover & Reserve Campsites Right From Gaia GPS

      February 22, 2022

  • New Maps
    • Backcountry Skiing

      New! Find Backcountry Skiing in Gaia Winter Map

      January 11, 2023

      Gaia GPS

      Our Favorite New Maps and Features

      December 23, 2022

      Gaia GPS

      Spy Avalanche Terrain with Higher Res Slope Angle…

      December 14, 2022

      Backcountry Skiing

      Find the Deepest, Lightest Powder with the Snow…

      November 8, 2022

      Gaia GPS

      Activate Snow Mode with Gaia Winter

      October 26, 2022

      Adventures

      Take a Trip to Baja California with Nat…

      September 13, 2022

      Gaia GPS

      Gaia Overland: One Map to Rule Them All

      May 24, 2022

  • Home

(Technical Post) How Memory Works on iOS

by Andrew Johnson May 26, 2011
by Andrew Johnson May 26, 2011

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.

memory managementObjective Cprogramming
0
FacebookTwitterRedditEmail
Andrew Johnson

Andrew founded Gaia GPS. He writes code and manages the business.

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

You may also like

How to Plan a Backcountry Ski Tour with...

January 26, 2023

Cook Marco Hernandez’s Mouthwatering Camp Meals

January 25, 2023

New! Find Backcountry Skiing in Gaia Winter Map

January 11, 2023

How to Get Started Overlanding

January 9, 2023

Our Favorite New Maps and Features

December 23, 2022

2022 Mapped: Gaia GPS Year in Review

December 22, 2022

Spy Avalanche Terrain with Higher Res Slope Angle...

December 14, 2022

How to Use Maps to Help Avoid Avalanches

December 14, 2022

How to Find a Christmas Tree Using Gaia...

December 1, 2022

Everything You Need to Know About Satellite Communicators

December 1, 2022

Keep in touch

Facebook Twitter Instagram Youtube

Popular Posts

  • 1

    How to Read Topographic Maps

    July 1, 2021
  • 2

    How to Download an Entire State Map with Gaia Topo

    August 19, 2021
  • 3

    How to Save Phone Battery Life in the Backcountry

    March 25, 2021
  • 4

    Using Gaia GPS to Find Free Camping in National Forests

    March 9, 2022
  • 5

    Unlock Adventure with Gaia GPS on Outside+

    September 30, 2021

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
  • User Profiles
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • RSS

@2022 - All Right Reserved. Gaia GPS


Back To Top