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

Clang 3.0 Memory Management

Clang 3.0 Memory Management

Vladimir Pouzanov

August 20, 2011
Tweet

More Decks by Vladimir Pouzanov

Other Decks in Programming

Transcript

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

    /%$, '012+3 ✤ 45+%10+3#*).02 )6%7.0 1")%70 ✤ 45+%10+3#*).3& '%()#*+ )),-%.
  2. Кратко об ARC ✤ 8%+ 9* retain-release, $% ✤ '7%:*

    ✤ )+063-;$** ✤ 1*$;<* .%(0 (-2 3(*$+3#$%=% 7*>"-;+0+0
  3. Кратко об 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
  4. Кратко об 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+;@
  5. 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& .%(
  6. Что же делает 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
  7. До и после -(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; }
  8. До и после -(void)setValue:(id)newValue { [value_ autorelease]; value_ = [newValue

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

    ... -(void)other { id val = [[self someData] retain]; ... [val release]; } -(id)someData { ... return data; } ... -(void)other { id val = [self someData]; ... }
  10. Ограничения ✤ 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)*...
  11. Конвенции ✤ 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
  12. Работа с существующим кодом ✤ 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 .%(*.
  13. 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+;
  14. __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
  15. Если переименовать метод очень сложно... @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.
  16. 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
  17. Слабые ссылки ✤ __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
  18. Производительность ✤ 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**
  19. 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