Video Tutorial: Objective-C Memory Management (Manual)

Before ARC, memory management in Objective-C was manual. Even with ARC it’s still important to understand how it works. By Ray Wenderlich.

Leave a rating/review
Save for later
Share

Contents

Hide contents

Video Tutorial: Objective-C Memory Management (Manual)

1 min

Challenge

Your challenge is to add a new “Closet” class to the demo project, using manual memory management, so that this code works:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    Person *ray = [[Person alloc] initWithName:@"Ray"];
    Person *vicki = [[Person alloc] initWithName:@"Vicki"];
    
    Closet *closet = [[Closet alloc] init];
    
    ray.sweater = closet.graySweaterSlot;
    closet.graySweaterSlot = nil;
    
    vicki.sweater = closet.blueSweaterSlot;
    closet.blueSweaterSlot = nil;

    closet.blueSweaterSlot = vicki.sweater;
    vicki.sweater = ray.sweater;
    ray.sweater = nil;

    [ray release];
    [vicki release];
    [closet release];
    return YES;
}

Download demo code

Download solution

Helpful links

Errata

  • Around 8:28, I said “you always have to call super dealloc first”. This is a bit confusing, because actually it’s best practice to put the call to [super dealloc] at the end of the method, as shown in the video. It would have been more clear if I had said “you always have to call super dealloc at the end of dealloc if you are not using ARC”.
  • Audio was cut off around the 15:00 mark – it should have said “when you call init, by convention, it returns an object with retain count of 1”.
Ray Wenderlich

Contributors

Ray Wenderlich

Author

Over 300 content creators. Join our team.