Leaks & Zombies

5,124 views

Published on

A BarCamp presentation about iPhone memory management programming. Teemu Kurppa gave this at MobileDevCamp Helsinki (http://MobileDevCamp.fi)

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
5,124
On SlideShare
0
From Embeds
0
Number of Embeds
1,077
Actions
Shares
0
Downloads
95
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

Leaks & Zombies

  1. 1. & Leaks Zombies A quick intro to iPhone memory management Teemu Kurppa Co-Founder, Huikea.com
  2. 2. Basics NSString* s = [[NSString alloc] init]; ➊ [s release];
  3. 3. Basics NSString* s = [[NSString alloc] init]; ➊ [s retain]; ➋ [s release]; ➊ [s release];
  4. 4. Autorelease NSString* s = [[[NSString alloc] init] autorelease]; ➊
  5. 5. Autorelease NSAutoreleasePool* pool = [[NSAutoreleasePool] alloc] init]; ... NSString* s = [[[NSString alloc] init] autorelease]; ➊ ... [pool release];
  6. 6. Factory methods NSString* s = [NSString string]; ➊ [pool release]; why? because: @implementation NSString + (NSString*)string { return [[[NSString alloc] init] autorelease]; } @end
  7. 7. An incorrect setter - (void)setFoo:(Bar*)aFoo { [_foo release]; _foo = [aFoo retain]; // Incorrect, why? }
  8. 8. A correct setter - (void)setFoo:(Bar*)aFoo { if (aFoo != _foo) { [_foo release]; _foo = [aFoo retain]; } }
  9. 9. A correct setter - (void)setFoo:(Bar*)aFoo { [_foo autorelease]; _foo = [aFoo retain]; }
  10. 10. 4 Rules of Thumb 1 Use autorelease for temporaries 2 Retain & release members 3 Use properties to get correct setters 4 Break rule 2 to avoid retain loops
  11. 11. Rule 1: Use autorelease for temporaries - (void) foo { NSString* s = [NSString string]; ➊ or NSString* s = [[[NSString alloc] init] autorelease]; ➊ }
  12. 12. Rule 2: Retain & release members @interface Bar { NSString* _name; } @end - (id) init { if (self = [super init]) { _name = [[NSString alloc] init]; } ➊ return self; } - (void) dealloc { [_name release]; [super dealloc]; }
  13. 13. Rule 3: Use properties to get correct setters @interface Bar { NSString* _name; } @property(retain) NSString* name; @end @implementation @synthesize name = _name; - (void) dealloc { [_name release]; // still important! [super dealloc]; }
  14. 14. Rule 4: Break rule 2 to avoid retain loops @interface Bar { id _delegate; } @property(assign) id delegate; // Note: not retain! @end @interface Foo { Bar* _bar; } @end in Foo: _bar = [[Bar alloc] init]; _bar.delegate = self;
  15. 15. What about bugs?
  16. 16. A leak - (void)init { if (self = [super init]) { _foo = [[NSString alloc] init]; ➊ } - (void)dealloc { [super dealloc]; }
  17. 17. Premature delete - (void)init { if (self = [super init]) { _foo = [NSString string]; } - (void)foo { NSLog(@”Foo is %@”, _foo); }
  18. 18. Double-deletion - (void) f { Bar* bar = [NSString string]; ➊ _foo = bar; } ... [pool release]; // - (void)dealloc { [_foo release]; !!! -1 [super dealloc]; }
  19. 19. Zombies to help NSZombieEnabled
  20. 20. Zombies to help NSZombieEnabled
  21. 21. by lunchtimemama http://www.flickr.com/photos/lunchtimemama/97685452/sizes/o/ by Rachel Cobcroft http://www.flickr.com/photos/felix42/453311029/sizes/o/

×