Your SlideShare is downloading. ×
Objective C Memory Management
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Objective C Memory Management

5,530
views

Published on

iOS 3 and iOS 4 Memory Management Tips and Tricks

iOS 3 and iOS 4 Memory Management Tips and Tricks

Published in: Technology

0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,530
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
190
Comments
0
Likes
7
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Objective-C Memory Management by Ahmed Magdy
  • 2. Introduction
    • Objective-C :
    • Is a strict superset of C
    • Is an OOP version of C using message passing.
    • Uses a variation of GCC and GDB
    • Has dynamic and static memory allocation
    • Uses “Reference Counting” to manage memory in iOS.
  • 3. OUTLINE
    • Reference counting
    • Object Ownership
    • Auto-release pools
    • Conventions
    • Properties in Objective-C
    • Rules of Thumb
    • Common mistakes
    • Optimizations
  • 4. Reference Counting
    • New object created with reference count=1
    • Send retain/release message to increase/decrease reference count
    • Object deallocated automatically when reference count becomes 0
  • 5. Object ownership
    • Any object may have one or more owner
    • Object’s creator becomes its 1st owner
    • Other owners must retain object
    • Owner is responsible for releasing object
    • Owner must never destroy object directly
    • Object without owners are deallocated automatically
    • Weak references used to avoid infinite retain loops
  • 6. Auto-release Pools
    • What is an Autorelease pool?
    • Use autorelease method when you don’t want to own the object
    • autorelease mean “Release later”
    • Every thread should have it’s own pool
  • 7. Auto-release pool example @implementation Person -(void)startPlayWithPets { [NSThread detachNewThreadSelector:@selector(play) toTarget:self withObject:nil]; } -(void)play { // without pool every autoreleased object will leak memory NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; // safe accessor returns autoreleased object, added to thread’s autorelease pool NSArray* petsToPlay = [self pets] ; BOOL allPetsAreHappy = NO; while(! allPetsAreHappy) { ... // some code, that may create autoreleased objects } [pool release]; // or [pool drain]; memory is freed, petsToPlay released as well } @end
  • 8. Conventions
    • To create an object and be the first owner of it, use a method that has one of these in its name: - alloc - new - copy - mutableCopy
    • If we use properties and convenience constructors we do not take ownership of the object.
        • - Convenience Constructors (Factory Pattern) like: stringWithFormat, imageWithData, imageWithContentsOfFile, stringWithContentsOfFile
  • 9. Properties in Objective-C
    • Writability (readonly, readwrite )
    • Setter semantic ( assign , copy, retain)
    • Atomicity ( atomic , nonatomic)
    • Declaration: @property (writability, setter, atomicity) type name; @synthesize name=ivarName;
    • Usage:
    • object.propertyName = newValue;
    • value = object.propertyName;
  • 10. Properties in Objective-C example 1 @property (nonatomic, retain) NSString* name; @synthesize name; // is equivalent to a pair of methods -(NSString*)name { return [[name retain] autorelease]; } -(void)setName:(NSString*)aName { if (name != aName) { [name release]; name = [aName retain]; } }
  • 11. Properties in Objective-C example 2 @property (atomic, copy) NSString* name; // equivalent to -(NSString*)name { NSString* retVal=nil; @synchronized(self) retVal = [[name retain] autorelease]; return retVal; } -(void)setName:(NSString*)aName { @synchronized(self) if (name != aName) { [name release]; name = [aName copy]; } }
  • 12. Rules of Thumb
    • Use autorelease for temporaries
    • Retain & release members
    • Use properties to get correct setters
    • Break rule 2 to avoid retain loops
  • 13. Use autorelease for temporaries - (void) foo { NSString* s = [NSString string]; Or NSString* s = [[[NSString alloc] init] autorelease]; }
  • 14. 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]; }
  • 15. 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 because of _name [super dealloc]; } @end
  • 16. 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;
  • 17. Common mistakes
    • Premature delete
    • Double-deletion
    • No retain/release balance
    • [self release]
    • Reassign of pointer without releasing old value
    • Overriding retain & release methods
  • 18. Premature Deletion - (void)init { if (self = [super init]) { _foo = [NSString string]; } } - (void)foo { NSLog(@”Foo is %@”, _foo); }
  • 19. Double-deletion - (void) f { Bar* bar = [NSString string]; _foo = bar; } ... [pool release] or [pool drain]; - (void)dealloc { [_foo release]; // retain count -1 [super dealloc]; }
  • 20. No retain/release balance
    • Causing either:
    • A memory leak , if the retain are greater than the releases
    • or
    • A dangling pointer , if the releases are greater than the retains. (Bad Access Error or accessing an illegal piece of memory)
  • 21. [self release] - (void) someMethod { _name = @”the name”; _message = [[[NSString alloc] initWithFormat: @”my name is: ”, _name] autorelease]; [self release]; NSLog(_message); // error, if self is deallocated } - (void) dealloc { [_name release]; [_message release]; [super dealloc]; }
  • 22. Reassign of pointer without releasing old value - (void) leak { NSString *name = [NSString alloc] initWithFormat:@”%@”, @”Ahmed”]; name = @”Ali”; // the value @”Ahmed” is still there so there is a memory leak }
  • 23. Overriding retain & release methods
    • Some developers tend to override retain and release methods.
    • They have to be very careful or else they may cause a memory leak or a dangling pointer.
  • 24. Optimizations
    • Use C structures instead of classes
    • Keep immutable objects
    • Release now instead of autorelease if possible
    • Nested Autorelease pools
    • Use UIKit recommendations
  • 25. Summary
    • Reference counting
    • Object Ownership
    • Auto-release pools
    • Conventions
    • Properties in Objective-C
    • Rules of Thumb
    • Common mistakes
    • Optimizations
  • 26. Thank you