Gaia GPS
  • Explore The Map
  • Get the App
  • Upgrade Today
  • Explore The Map Catalog
  • New Features
    • Gaia GPS

      The Hike Map That Broke Me: How Gaia…

      April 24, 2025

      Gaia GPS

      Introducing the Gaia Hike Map: Your Trail-Ready Adventure…

      March 18, 2025

      Gaia GPS

      Unlock a New Level of Personalization in Gaia…

      December 12, 2024

      Gaia GPS

      Goodbye Clutter, Hello Streamlined Maps: Introducing Sync to…

      November 26, 2024

      Gaia GPS

      Discover Prime Stargazing Locations with Our New Light…

      August 15, 2024

      Gaia GPS

      2023 Mapped: Our Best New Features of the…

      December 27, 2023

      Gaia GPS

      Discover Adventure Easier Than Ever with New Map…

      July 27, 2023

    • New Maps
      • Gaia GPS

        Discover Prime Stargazing Locations with Our New Light…

        August 15, 2024

        Gaia GPS

        Find Prime Viewing for Total Solar Eclipse with…

        March 27, 2024

        Gaia GPS

        See the World More Clearly with New Gaia…

        May 18, 2023

        Gaia GPS

        Gaia Classic: The Only Map You’ll Ever Need?

        May 4, 2023

        Gaia GPS

        Why NatGeo Trails Illustrated are America’s Favorite Maps

        February 9, 2023

        Backcountry Skiing

        Find Backcountry Skiing in Gaia Winter Map

        January 11, 2023

        Gaia GPS

        Our Favorite New Maps and Features

        December 23, 2022

  • Activities
    • Backcountry Skiing
    • Boating
    • Emergency Response
    • Fishing
    • Offroading
  • Adventures
    • User Stories
  • Help
Top Posts
The Hike Map That Broke Me: How Gaia...
A Fond Farewell to National Geographic Maps —...
Introducing the Gaia Hike Map: Your Trail-Ready Adventure...
Download the app and get a free 14-day...
Gaia GPS is Improving Satellite Imagery: Saying Goodbye...
How I Used Gaia GPS to Navigate Italy
Gaia GPS x Toyota: A New Way to...
Download Gaia GPS– iOS & Android App
Unlock a New Level of Personalization in Gaia...
Important Update: Changes to Esri World Imagery Offline...
Gaia GPS
  • Explore The Map
  • Get the App
  • Upgrade Today
  • Explore The Map Catalog
  • New Features
    • Gaia GPS

      The Hike Map That Broke Me: How Gaia…

      April 24, 2025

      Gaia GPS

      Introducing the Gaia Hike Map: Your Trail-Ready Adventure…

      March 18, 2025

      Gaia GPS

      Unlock a New Level of Personalization in Gaia…

      December 12, 2024

      Gaia GPS

      Goodbye Clutter, Hello Streamlined Maps: Introducing Sync to…

      November 26, 2024

      Gaia GPS

      Discover Prime Stargazing Locations with Our New Light…

      August 15, 2024

      Gaia GPS

      2023 Mapped: Our Best New Features of the…

      December 27, 2023

      Gaia GPS

      Discover Adventure Easier Than Ever with New Map…

      July 27, 2023

    • New Maps
      • Gaia GPS

        Discover Prime Stargazing Locations with Our New Light…

        August 15, 2024

        Gaia GPS

        Find Prime Viewing for Total Solar Eclipse with…

        March 27, 2024

        Gaia GPS

        See the World More Clearly with New Gaia…

        May 18, 2023

        Gaia GPS

        Gaia Classic: The Only Map You’ll Ever Need?

        May 4, 2023

        Gaia GPS

        Why NatGeo Trails Illustrated are America’s Favorite Maps

        February 9, 2023

        Backcountry Skiing

        Find Backcountry Skiing in Gaia Winter Map

        January 11, 2023

        Gaia GPS

        Our Favorite New Maps and Features

        December 23, 2022

  • 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

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

The Hike Map That Broke Me: How Gaia...

April 24, 2025

A Fond Farewell to National Geographic Maps —...

March 26, 2025

Introducing the Gaia Hike Map: Your Trail-Ready Adventure...

March 18, 2025

Download the app and get a free 14-day...

March 1, 2025

Gaia GPS is Improving Satellite Imagery: Saying Goodbye...

February 24, 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
  • User Profiles

Recent Posts

  • The Hike Map That Broke Me: How Gaia GPS Turned an Overlander Into a Reluctant Hiker
  • A Fond Farewell to National Geographic Maps — And a Look at What’s Ahead
  • Introducing the Gaia Hike Map: Your Trail-Ready Adventure Buddy
  • Download the app and get a free 14-day trial of Gaia GPS Premium
    • Facebook
    • Twitter
    • Instagram
    • Youtube
    • RSS
    • Explore The Map
    • Get the App
    • Upgrade Today
    • Explore The Map Catalog
    • New Features
      • Gaia GPS

        The Hike Map That Broke Me: How Gaia…

        April 24, 2025

        Gaia GPS

        Introducing the Gaia Hike Map: Your Trail-Ready Adventure…

        March 18, 2025

        Gaia GPS

        Unlock a New Level of Personalization in Gaia…

        December 12, 2024

        Gaia GPS

        Goodbye Clutter, Hello Streamlined Maps: Introducing Sync to…

        November 26, 2024

        Gaia GPS

        Discover Prime Stargazing Locations with Our New Light…

        August 15, 2024

        Gaia GPS

        2023 Mapped: Our Best New Features of the…

        December 27, 2023

        Gaia GPS

        Discover Adventure Easier Than Ever with New Map…

        July 27, 2023

      • New Maps
        • Gaia GPS

          Discover Prime Stargazing Locations with Our New Light…

          August 15, 2024

          Gaia GPS

          Find Prime Viewing for Total Solar Eclipse with…

          March 27, 2024

          Gaia GPS

          See the World More Clearly with New Gaia…

          May 18, 2023

          Gaia GPS

          Gaia Classic: The Only Map You’ll Ever Need?

          May 4, 2023

          Gaia GPS

          Why NatGeo Trails Illustrated are America’s Favorite Maps

          February 9, 2023

          Backcountry Skiing

          Find Backcountry Skiing in Gaia Winter Map

          January 11, 2023

          Gaia GPS

          Our Favorite New Maps and Features

          December 23, 2022

    • Activities
      • Backcountry Skiing
      • Boating
      • Emergency Response
      • Fishing
      • Offroading
    • Adventures
      • User Stories
    • Help

    @2024 - All Right Reserved. Gaia GPS


    Back To Top