Upgrade to Pro — share decks privately, control downloads, hide ads and more …

iOS memory management

iOS memory management

Cyril Lashkevich

March 13, 2014
Tweet

More Decks by Cyril Lashkevich

Other Decks in Programming

Transcript

  1. • Three magic words • alloc* • new* • *copy

    MRC (pre ARC) piątek, 31 maja 13
  2. • Three magic words • alloc* • new* • *copy

    • retain/release/autorelease MRC (pre ARC) piątek, 31 maja 13
  3. • Three magic words • alloc* • new* • *copy

    • retain/release/autorelease • 2 questions MRC (pre ARC) piątek, 31 maja 13
  4. • Three magic words • alloc* • new* • *copy

    • retain/release/autorelease • 2 questions MRC (pre ARC) • Where is reference count stored? piątek, 31 maja 13
  5. • Three magic words • alloc* • new* • *copy

    • retain/release/autorelease • 2 questions MRC (pre ARC) • Where is reference count stored? • Not in NSObject. piątek, 31 maja 13
  6. • Three magic words • alloc* • new* • *copy

    • retain/release/autorelease • 2 questions MRC (pre ARC) • Where is reference count stored? • Not in NSObject. • How does autorelease pool work? piątek, 31 maja 13
  7. ARC • It just works. • It is not a

    kind of GC • It is smarter then you • It is faster piątek, 31 maja 13
  8. It just works - (id)init { if (self = [super

    init];) { self.wallet = [[[Wallet alloc] init] autorelease]; } return self; } - (void)dealloc { self.wallet = nil; [super dealloc]; } piątek, 31 maja 13
  9. It just works - (id)init { if (self = [super

    init];) { self.wallet = [[Wallet alloc] init]; } return self; } piątek, 31 maja 13
  10. Not a kind of GC •weak, strong(retain), copy, unsafe_unretained(assign) •__weak,

    __strong, __unsafe_unretained • IBOutlets should generally be weak except File’s Owner • Weak objects should not override release • __autoreleased for pass-by-writeback (id *) and for objects thats are autoreleased on return. piątek, 31 maja 13
  11. Toll-free bridging • id obj = (__bridge id)CFDictionaryGetValue(cfDict, key); •

    NSString *value = (__bridge_transfer NSString *)CFPreferencesCopyAppValue(CFSTR("someKey"), CFSTR("com.company.someapp")); [self useValue: value]; • CFStringRef value = (__bridge_retained CFStringRef) [self someString]; UseCFStringValue(value); CFRelease(value); piątek, 31 maja 13
  12. Return values and toll- free bridging •- (CGColorRef)getFooColor { UIColor*

    __autoreleasing color = [UIColor redColor]; return [color CGColor]; } •- (CGColorRef)fooColorCopy { UIColor* color = [UIColor redColor]; CGColorRef c = CFRetain([color CGColor]); return c; } piątek, 31 maja 13
  13. ARC is safer Foo *foo = [self foo]; [foo bar];

    [self baz]; [foo quux]; piątek, 31 maja 13
  14. ARC is safer Foo *foo = [self foo]; [foo bar];

    [self baz]; [foo quux]; Foo *foo = [self foo]; [foo bar]; [self setFoo: newFoo]; [foo quux]; // crash piątek, 31 maja 13
  15. Foo *foo = objc_retainAutoreleasedReturnValue([self foo]); [foo bar]; [self baz]; [self

    setFoo: newFoo]; [foo quux]; // fine objc_release(foo); - (Foo *)foo { return objc_retainAutoreleaseReturnValue(_foo); } piątek, 31 maja 13
  16. • All objects passed as arguments are retained at the

    beginning of the function and released at the end • All values from getters are retained and released at the end of variable scope piątek, 31 maja 13
  17. Memory management and blocks. • Use __weak, not __block to

    capture self • Copy block when passing 'down' to stack. Passing 'up' compiler handle automaticly. piątek, 31 maja 13