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

Clang 3.0 Memory Management

Clang 3.0 Memory Management

Abc432bb0450574a25b15ff23d399d5a?s=128

Vladimir Pouzanov

August 20, 2011
Tweet

Transcript

  1. © 2011 Vladimir “Farcaller” Pouzanov <farcaller@gmail.com> Clang 3.0 Memory Management

    Reference counting done right!
  2. Как это было ✤ new-free ✤ !"#$%& '%()#*+ )),-%. ✤

    /%$, '012+3 ✤ 45+%10+3#*).02 )6%7.0 1")%70 ✤ 45+%10+3#*).3& '%()#*+ )),-%.
  3. Встречайте: Automatic Reference Counting

  4. Кратко об ARC ✤ 8%+ 9* retain-release, $% ✤ '7%:*

    ✤ )+063-;$** ✤ 1*$;<* .%(0 (-2 3(*$+3#$%=% 7*>"-;+0+0
  5. Кратко об ARC ✤ ?-@),: ✤ 5,)%.02 ABB*.+35$%)+; retain-release 1*C0$3>10

    ✤ D%'%-$3+*-;$,* '-@),: ✤ $* $0(% +0. )3-;$% $0'72=0+; 1%>= (-2 (%)+39*$32 A.5350-*$+$%=% 7*>"-;+0+0
  6. Кратко об ARC ✤ ARC A+% $*... ✤ $%502 1%(*-;

    "'705-*$32 '012+;@ ✤ )6%7:3. 1")%70 ✤ >01*$0 .%(", .%+%7,& 3)'%-;>"*+ API libc 3/3-3 CF (-2 "'705-*$32 '012+;@
  7. Clang Static Analyzer ✤ 4$0-3>0+%7 )":*)+5"*+ "9* $*).%-;.% 5*7)3& SDK

    ✤ 8%+ 9* $06%7 >0(0# '% 0$0-3>" retain-release .%(0 ✤ E*$0 %<36.3: -%9$%* )7060+,50$3* vs. $*706%#3& .%(
  8. Что же делает ARC? ✤ F*$*70G32 retain-release .%(0 ✤ ?%((*79.0

    %6:3C Cocoa-.%$5*$G3& $0 +*1" "'705-*$32 '012+;@ ✤ ?%-$02 )%51*)+31%)+; ) .%(%1, .%+%7,& 706%+0*+ 6*> ARC
  9. ARC в Clang 3.0

  10. До и после -(id)initWithData:(NSData*)aData { if(self=[super init]){ data_ = [aData

    retain]; } return self; } -(id)initWithData:(NSData*)aData { if(self=[super init]){ data_ = aData; } return self; }
  11. До и после -(void)setValue:(id)newValue { [value_ autorelease]; value_ = [newValue

    retain]; } -(void)setValue:(id)newValue { value_ = newValue; }
  12. До и после -(id)someData { ... return [data autorelease]; }

    ... -(void)other { id val = [[self someData] retain]; ... [val release]; } -(id)someData { ... return data; } ... -(void)other { id val = [self someData]; ... }
  13. Управление связями ✤ F70B %6H*.+%5 .0. 1*C0$3>1 05+%10+3#*).%=% "'705-*$32 '012+;@

  14. Ограничения ✤ I .%$+*.)+* ARC >0'7*:*$% 3)'%-;>%50+; retain/release/ autorelease/retainCount ✤

    /0'7*:*$% 3)'%-;>%50+; id 3 '7%3>5%($,* 5 struct ✤ .7%1* __unsafe_unretained id ✤ J*%6C%(31% 57"#$"@ ".0>,50+; '*7*1*:*$32 '705 5-0(*$32 1*9(" id 3 void* (Cocoa ↔ CoreFoundation) ✤ /0'7*:*$% 3)'%-;>%50+; %6H*.+ NSAutoreleasePool ✤ C%+2 A+% 3 $* %6H*.+ 5%5)*...
  15. Конвенции ✤ ARC 3)'%-;>"*+ 5)* +* 9* .%$5*$G33 $0>50$3& )*-*.+%7%5

    ✤ I)* 1*+%(,, $0>50$3* .%+%7,C !"#$!"%&'( ) ✤ alloc, init, copy, mutableCopy, new ✤ (%-9$, 5%>570:0+; >$0#*$3* ) +1 retainCount ✤ I)* %)+0-;$,* 1*+%(, 5%>570:0@+ )60-0$)37%50$$,* >$0#*$32
  16. Перенос текущего кода на ARC

  17. Опциональное использование ARC ✤ K-0=3 .%1'3-2+%70 ✤ -fobjc-arc ✤ -fno-objc-arc

    ✤ F-%60-;$02 $0)+7%&.0 '7%*.+0
  18. Работа с существующим кодом ✤ I)* 706%+0*+* ✤ ?73 5>031%(*&)+533

    ) $*-arc .%(%1 3)'%-;>"@+)2 +* 9* .%$5*$G33 * 706%+0*+, $% $* 5)*. /053)3+ %+ 3)'%-;>"*1,C $0>50$3& 1*+%(%5. L%9*+ 5,>,50+; )'%$+0$$,* 3).-@#3+*-;$,* )3+"0G33. J*(%)+"'$% 5 llvm-gcc. J* 706%+0*+ $0 iOS3. D%.-0(#3. $* $*)*+ %+5*+)+5*$$%)+3 >0 .7*<3 5 50<*1 .%(*.
  19. Refactor → Convert to Objective-C ARC... ✤ ?%-"05+%10+3#*).3& 3$+*7B*&) (-2

    '*7*$%)0 .%(0 $0 ARC ✤ M%1'3-37"*+ ) ARC, )01 3)'705-2*+ %<36.3, .%+%7,* 1%9*+ ✤ N)+05-2*+ 501 +%, ) #*1 $* 1%9*+ )'7053+;)2 )01 ✤ 5 %)$%5$%1, )52>0$% ) '7%'":*$$,13 )*-*.+%7013 3 __weak ✤ O1,+;, '%5+%73+;
  20. __weak fail ✤ __weak '%253-)2 5% 57*1*$0 Objective-C GC ✤

    I$* GC .%(0 (5 +%1 #3)-* $0 iOS) '7%)+% 3=$%737%50-)2 ✤ D% ARC! ✤ 8*'*7; __weak )'%)%6*$ >0=$0+; 50) 5 %<36." .%1'3-2G33, *)-3 5, )%6370*+* .%( (-2 iOS4
  21. Если переименовать метод очень сложно... @interface MyClass : NSObject -

    (id)generateSchema; // возвращает объект с +1 retainCount @end @interface MyClass : NSObject - (id)generateSchema NS_RETURNS_RETAINED; @end This macro is ONLY to be used in exceptional circumstances, not to annotate functions which conform to the Cocoa naming rules.
  22. Objective-C Runtime & ARC

  23. ARC Runtime ✤ Snow Leopard / iOS4 ✤ ARC (%)+"'*$

    3 %+-3#$% 706%+0*+, '%).%-;." >053)3+ %+ .%1'3-2+%70 ✤ Lion / iOS5 ✤ ARC 706%+0*+ *:* -"#<* – )'*G30-;$,* %'+313>0G33 70$+0&10 ✤ ARC '%((*79350*+ 05+%10+3#*).3 %6$"-2*1,* )-06,* )),-.3
  24. Слабые ссылки ✤ __weak – )-0602 05+%10+3#*).3 %6$"-2*102 )),-.0 ✤

    __unsafe_unretained – 0$0-%= assign property ✤ MAZeroingWeakRef "9* 0(0'+37%50$ '%( 3)'%-;>%50$3* objc_loadWeak/objc_storeWeak, *)-3 %$3 (%)+"'$, https://github.com/mikeash/MAZeroingWeakRef
  25. Производительность ✤ objc_retain/objc_release 706%+0@+ 5 2-2,5 70>0 6,)+7** ✤ @autoreleasepool

    { ... } 706%+0*+ (% <*)+3 70> 6,)+7** ✤ objc_msgSend 706%+0*+ $0 33% 6,)+7** ✤ '07, autorelease-retain $0 )+*.* 706%+0@+ (% 20 70> 6,)+7**
  26. One more thing...

  27. Instruments ✤ Instruments '%((*79350*+ %+-0(." retain-G3.-%5 5 ARC 3 $*-ARC

    .%(* 3 3C 5>031%(*&)+533
  28. http://farcaller.net/it-jam-arc Спасибо! I%'7%),? @annostapova, '%((*79.0 5 $0'3)0$33 )-0&(%5 @iurii_ua, %#*$;

    '%1%= 70>7"-3+; .%)2. prostoprint @pfactum, -"#<3& =701107-$0G3 @darkproger, A$+">30>1 3 1%+350G32 /01*#0+*-;$02 0"(3+%732