Gaia GPS
  • Home
  • Adventures
    • User Stories
    • Activities
      • Backcountry Skiing
      • Boating
      • Emergency Response
      • Fishing
      • Offroading
  • Out and Back Podcast
    • Gaia GPS

      Climber and Entrepreneur Matt Segal on Finding Balance

      February 17, 2021

      Gaia GPS

      Camping and Overlanding Save Atlanta Couple’s Marriage

      February 11, 2021

      Adventures

      Backcountry Skiing in the Himalayas with Mountain Guide…

      February 3, 2021

      Backcountry Skiing

      The Best Backcountry Skiing Gear Recommendations

      January 19, 2021

      Gaia GPS

      Earn Your Turns: Learn How to Backcountry Ski…

      January 4, 2021

      Gaia GPS

      Lessons from the Trail: Best Out and Back…

      December 28, 2020

      Gaia GPS

      Buried Alive: Bruce Tremper Shares His Avalanche Story

      December 21, 2020

  • News
    • New Maps
    • New Features
    • Gaia GPS News
  • Shop Swag
Gaia GPS
  • Home
  • Adventures
    • User Stories
    • Activities
      • Backcountry Skiing
      • Boating
      • Emergency Response
      • Fishing
      • Offroading
  • Out and Back Podcast
    • Gaia GPS

      Climber and Entrepreneur Matt Segal on Finding Balance

      February 17, 2021

      Gaia GPS

      Camping and Overlanding Save Atlanta Couple’s Marriage

      February 11, 2021

      Adventures

      Backcountry Skiing in the Himalayas with Mountain Guide…

      February 3, 2021

      Backcountry Skiing

      The Best Backcountry Skiing Gear Recommendations

      January 19, 2021

      Gaia GPS

      Earn Your Turns: Learn How to Backcountry Ski…

      January 4, 2021

      Gaia GPS

      Lessons from the Trail: Best Out and Back…

      December 28, 2020

      Gaia GPS

      Buried Alive: Bruce Tremper Shares His Avalanche Story

      December 21, 2020

  • News
    • New Maps
    • New Features
    • Gaia GPS News
  • Shop Swag

(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

Get Worldwide 3D Maps on the Web at...

February 25, 2021

How to Use Gaia GPS for Snowmobiling

February 25, 2021

Gaia GPS Joins Forces with Outside to Transform...

February 24, 2021

How to Use Maps to Help Avoid Avalanches

February 18, 2021

The Art of Layering : What to Wear...

February 18, 2021

Climber and Entrepreneur Matt Segal on Finding Balance

February 17, 2021

Get 2,400+ Overlanding Routes with the New Trails...

February 11, 2021

Camping and Overlanding Save Atlanta Couple’s Marriage

February 11, 2021

Winter Hiking Safety Guide: How to Stay Warm...

February 4, 2021

Backcountry Skiing in the Himalayas with Mountain Guide...

February 3, 2021

Keep in touch

Facebook Twitter Instagram Youtube

Popular Posts

  • 1

    How to Read Topographic Maps

    October 7, 2020
  • 2

    Forrest Fenn’s Treasure has been Found

    March 12, 2019
  • 3

    How to Save Phone Battery Life in the Backcountry

    August 24, 2020
  • 4

    Gaia GPS and Apple CarPlay Bring Outdoor Maps to Your Dashboard

    February 24, 2020
  • 5

    Easy to Read, Tiny to Download: The All New Gaia Topo

    January 28, 2020

Categories

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

@2020 - All Right Reserved. Gaia GPS


Back To Top