Leaks & Zombies

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

Post a comment
Embed Video
Edit your comment Cancel

Favorites, Groups & Events

Leaks & Zombies - Presentation Transcript

  1. & Leaks Zombies A quick intro to iPhone memory management Teemu Kurppa Co-Founder, Huikea.com
  2. Basics NSString* s = [[NSString alloc] init]; ➊ [s release];
  3. Basics NSString* s = [[NSString alloc] init]; ➊ [s retain]; ➋ [s release]; ➊ [s release];
  4. Autorelease NSString* s = [[[NSString alloc] init] autorelease]; ➊
  5. Autorelease NSAutoreleasePool* pool = [[NSAutoreleasePool] alloc] init]; ... NSString* s = [[[NSString alloc] init] autorelease]; ➊ ... [pool release];
  6. Factory methods NSString* s = [NSString string]; ➊ [pool release]; why? because: @implementation NSString + (NSString*)string { return [[[NSString alloc] init] autorelease]; } @end
  7. An incorrect setter - (void)setFoo:(Bar*)aFoo { [_foo release]; _foo = [aFoo retain]; // Incorrect, why? }
  8. A correct setter - (void)setFoo:(Bar*)aFoo { if (aFoo != _foo) { [_foo release]; _foo = [aFoo retain]; } }
  9. A correct setter - (void)setFoo:(Bar*)aFoo { [_foo autorelease]; _foo = [aFoo retain]; }
  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. Rule 1: Use autorelease for temporaries - (void) foo { NSString* s = [NSString string]; ➊ or NSString* s = [[[NSString alloc] init] autorelease]; ➊ }
  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. 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. 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. What about bugs?
  16. A leak - (void)init { if (self = [super init]) { _foo = [[NSString alloc] init]; ➊ } - (void)dealloc { [super dealloc]; }
  17. Premature delete - (void)init { if (self = [super init]) { _foo = [NSString string]; } - (void)foo { NSLog(@”Foo is %@”, _foo); }
  18. Double-deletion - (void) f { Bar* bar = [NSString string]; ➊ _foo = bar; } ... [pool release]; // - (void)dealloc { [_foo release]; !!! -1 [super dealloc]; }
  19. Zombies to help NSZombieEnabled
  20. Zombies to help NSZombieEnabled
  21. by lunchtimemama http://www.flickr.com/photos/lunchtimemama/97685452/sizes/o/ by Rachel Cobcroft http://www.flickr.com/photos/felix42/453311029/sizes/o/
SlideShare Zeitgeist 2009

+ Teemu KurppaTeemu Kurppa Nominate

custom

1265 views, 0 favs, 4 embeds more stats

A BarCamp presentation about iPhone memory manageme more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 1265
    • 1069 on SlideShare
    • 196 from embeds
  • Comments 1
  • Favorites 0
  • Downloads 3
Most viewed embeds
  • 193 views on http://dirtyaura.org
  • 1 views on http://74.125.43.132
  • 1 views on http://74.125.155.132
  • 1 views on http://draft.blogger.com

more

All embeds
  • 193 views on http://dirtyaura.org
  • 1 views on http://74.125.43.132
  • 1 views on http://74.125.155.132
  • 1 views on http://draft.blogger.com

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories