Memory Management

Memory Management

Lightening talk done at NSLondon about Memory Management. Covering many of the common mistakes people have with memory management using ARC and how to avoid. Briefly covering the runtime support for ARC.

Related blog post: http://kylefuller.co.uk/posts/memory-management-arc/

D200a17dd269fd4001bacb11662dab4b?s=128

Kyle Fuller

October 24, 2013
Tweet

Transcript

  1. Memory Management

  2. -[NSObject retainCount]

  3. Retainable Object Pointers

  4. Object Pointers id Class NSObject __attribute__((NSObject))

  5. __attribute__((NSObject))

  6. dispatch_queue_t

  7. @property (nonatomic, strong) dispatch_queue_t queue;

  8. Ownership Qualification

  9. __unsafe_unretained

  10. __strong

  11. __autoreleasing

  12. __weak

  13. id objc_storeWeak(id *object, id value)

  14. objc_clear_deallocating

  15. objc_arc_weak_unavailable

  16. Property Declarations assign __unsafe_unretained copy -copy + __strong retain __strong

    unsafe_unretained __unsafe_unretained weak __weak
  17. Blocks

  18. `strong` vs `copy`

  19. Always use copy

  20. @property (nonatomic, copy)

  21. dispatch_block_t block = ^{ NSLog(@"Hello World!"); }; ! NSArray *blocks

    = [[NSArray alloc] initWithObject:[block copy]];
  22. Retain Cycle

  23. - (void)startOperation { NSOperation *operation = [[NSOperation alloc] init]; !

    [operation setCompletionBlock:^{ NSLog(@"Completion for %@", operation); }]; }
  24. Operation Completion Handler

  25. - (void)startOperation { NSOperation *operation = [[NSOperation alloc] init]; __weak

    NSOperation *weakOperation = operation; ! [operation setCompletionBlock:^{ NSLog(@"Completion for %@", weakOperation); }]; }
  26. The Dealloc Problem

  27. Releasing UIKit objects in background threads

  28. CoreFoundation is not subject to ARC

  29. CFRelease(stringRef);

  30. UIColor *whiteColor = [UIColor whiteColor]; CGColorRef whiteRef = [whiteColor CGColor];

    ! // Crash when using whiteRef
  31. UIColor *whiteColor = [UIColor whiteColor]; CGColorRef whiteRef = CGRetain([whiteColor CGColor]);

    ! // Use whiteRef ! CGRelease(whiteRef);
  32. Exceptions

  33. -fobjc-arc-exceptions

  34. Fast Enumeration

  35. for (const __strong NSObject *object in objects) { NSLog(@”Object: %@”,

    object); }
  36. Faster Enumeration

  37. for (const __unsafe_unretained NSObject *object in objects) { NSLog(@”Object: %@”,

    object); }
  38. Does this actually have any real world benefits?

  39. None
  40. KFNumber

  41. Tagged Pointer

  42. None
  43. Twitter: @kylefuller GitHub: kylef ! http://kylefuller.co.uk/