2012年5月6日 星期日

XCode4.2下的物件參照 weak and strong


Strong 與 Weak
  • Strong 的參考上是參考到一個物件一直到當該物件被 deallocted,也就是會幫我們建立出彼此的關聯性,建立彼此的擁有權生命週期。 
  • Weak 的參考上是一直對應到該物件,就算這個物件被 dealloc 了還是存在。所以它不會建立擁有權。
  • _strong 是預設值,所以不打出來就是這樣的方式,建立了物件就會幫忙處理所有的 retained 和 released,自己內部這個物件使用。
  • _weak 代表這個物件可以隨時不見都沒有關係,如果對應到某個物件就算被 dealloc 它就會變成 nil 。
  • unsafe_unretained 這是跟 weak 一樣,但是當物件被 dealloc 不會將指標變成 nil。只是會變成指到無效的物件。
  • _autoreleasing 這會指引到該物件並且建立 autorelease 關係。

原文參考
http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

此處還需另外參考ARC Overview

strong也就是我们通常所讲的引用,其存亡直接决定了所指对象的存亡。如果不存在指向一个对象的引用,并且此对象不再显示列表中,则此对象会被从内存中释放。
  weak除了不决定对象的存亡外,其他与强引用相同。即使一个对象被持有无数个若引用,只要没有强引用指向他,那麽其还是会被清除。没办法,还是 “强哥” 有面子。

只要沒有strong pointer 指到的物件就會被清除。


簡單說來,在XCode4.2的機制下物件有分為兩種,一種是weak的參照,一種是strong的參照。weak的參照就像是一般的C下面的記憶體參照, 純粹就是一個記憶體的指標,指向一個記憶體中的物件。同時在自動的參照記數下,如果它所指向的物件在空間中的被移除配置(deallocate)了之後,它的值也會被設成nil,這樣才不會變成一個指向不合法空間的指標。因此你可以想像這種weak的用法就像是以前的assign,只是現在的記憶體管理機制會幫你把這個變數的參照設定成nil。

而相較於weak,strong所代表的就像是我們所熟悉的retain,因此你可以確保這個成員變數在它的母物件尚未被釋放前都是依然有效的。

其中strong,通過pointer,將資料放在堆疊中,而不是放在一般的記憶體中。如果pointer改指向nil,計數就會減一。或是將資料從堆疊中取出,計數也會減一。

在記憶體管理中,會有一個計數器計數堆疊數,當計數回到0的時候就會釋放記憶體。 
一般的local variables就是strong pointer


weak只在iOS5下面起作用,iOS4不會將pointer自動歸0,因此只能用 strong,手動將它歸到0。




strong/weak 是 ARC 用的,retain/copy/weak 是 non-ARC 用的。
strong, retain, copy 都是說我對這個 property 有 ownership。例如一個 Person Class 有 name property 就是有 ownership。retain 和 copy 是說,當你用 setter 時,採用的是 retain 或者是 copy。用 copy 的時機是避免 mutability,例如說 obj.name = aMutableString; [aMutableString truncate]; 我們設定了 name 以後,別人不小心在外面動到 mutable string,居然把我這個 object 的名字也改掉了,所以我們希望 copy 一份讓別人不要動到。而 strong 算是新一代的宣示,他就是「我有 ownership」,至於細節怎麼處理,就是由 compiler ARC 去做了。
weak 或 assign 則是我對此 property 沒有 ownership。例如 delegate 通常都用 weak/assign。因為一個搞不好很容易變成 retain cycle,就會有 memory leak。weak 還有個好處,就是當被指到的對象 dealloc'd 時會自動 zeroing property。
至於那段話意思是說,假設你 synthesize 一個 NSArray property 好了,他只是會幫你生一個 NSArray * pointer,實際上這 pointer 的值還是 nil,你要產生真正可以用的 NSArray instance 要自己去 create。
就好像 NSArray *a = nil; a = [NSArray new]; 這 synthesize 幫你產生前半句,後半句你要自己做的意思。


轉貼自
 http://www.runpc.com.tw/content/content.aspx?id=108343
http://ask.inside.com.tw/questions/98-ios5-%E4%B8%AD-property%E7%9A%84%E5%AE%A3%E5%91%8A%E6%9C%89%E6%B2%92%E6%9C%89%E6%9B%B4%E6%B8%85%E6%A5%9A%E7%9A%84%E8%AA%AA%E6%B3%95


Automatic Reference Counting

Automatic Reference Counting (ARC) is a compiler-level feature that simplifies the process of managing the lifetimes of Objective-C objects. Instead of you having to remember when to retain or release an object, ARC evaluates the lifetime requirements of your objects and automatically inserts the appropriate method calls at compile time.


To be able to deliver these features, ARC imposes some restricti*****—primarily enforcing some best practices and disallowing some other practices:

Do not call the retain, release, autorelease, or dealloc methods in your code.
In addition, you cannot implement custom retain or release methods.

Because you do not call the release method, there is often no need to implement a custom dealloc method—the compiler synthesizes all that is required to relinquish ownership of instance variables. You can provide a custom implementation of dealloc if you need to manage other resources.

Do not store object pointers in C structures.
Store object pointers in other objects instead of in structures.

Do not directly cast between object and nonobject types (for example, between id and void*).
You must use special functi***** or casts that tell the compiler about an object’s lifetime. You use these to cast between Objective-C objects and Core Foundation objects.

You cannot use NSAutoreleasePool objects.
Instead, you must use a new @autoreleasepool keyword to mark the start of an autorelease block. The contents of the block are enclosed by curly braces, as shown in the following example:

@autoreleasepool
{
   // Your code here
}
ARC encourages you to think in terms of object graphs, and the relati*****hips between objects, rather than in terms of retain and release. For this reason, ARC introduces new lifetime qualifiers for objects, including zeroing weak references. The value of a zeroing weak reference is automatically set to nil if the object to which it points is deallocated. There are qualifiers for variables, and new weak and strong declared property attributes, as illustrated in the following examples:

// The following declaration is a synonym for: @property(retain) MyClass *myObject;
@property(strong) MyClass *myObject;

// The following declaration is similar to "@property(assign) MyOtherClass *delegate;"
// except that if the MyOtherClass instance is deallocated,
// the property value is set to nil instead of remaining as a dangling pointer
@property(weak) MyOtherClass *delegate;
Xcode provides migration tools to help convert existing projects to use ARC. For more information about how to perform this migration, see Xcode New Features User Guide. For more information about ARC itself, see Programming With ARC Release Notes.

image: ../Art/memory_management.jpg





image: ../Art/ARC_Illustration.jpg