2

I'm newbie to objective-C, I feel comportable in C++.

My question is: Why language designer of obj-c proper to use retain/release rather then use new/delete(=alloc/dealloc) only?

Maybe my brain is fit to new/delete only memory management, I can not understand why I should manage reference counts, I think I know when object have to be alloc/dealloc with my C++ experence.

(Yes, I spend 4 hours to debug reference count problem, it is resolved by 1 line "release")

Can anyone explain me what is better when we use reference counter? (in programming language respects) I think I can manage lifecycle of object by new/delete, but I can't with reference counting.

I need long article that explains why reference counter is useful, if you have link.

P.S: I heard about Compile-time Automatic Reference Counting at WWDC 2011, it is really awesome, it can be reason of use of reference counter, for example.

4

1 に答える 1

8

簡単に言うと、C ++の場合のように「所有権」を必要とせずに、オブジェクトの存続期間を管理する方法です。

C ++でを使用newしてオブジェクトを作成する場合、後でそのオブジェクトをいつ作成するかを知る必要deleteがあります。多くの場合、これは簡単ですが、オブジェクトがさまざまなライフタイムを持つ多くの異なる「所有者」によって渡され、共有される可能性がある場合、困難になる可能性があります。

参照カウントでは、他のオブジェクトがそのオブジェクトを参照している限り、そのオブジェクトは存続します。他のすべてのオブジェクトが参照を削除すると、その参照は表示されなくなります。このアプローチには欠点があります(保持/解放および参照サイクルのデバッグが最も明白です)が、完全自動のガベージコレクションに代わる便利な方法です。

Objective-Cは、参照カウントを使用する唯一の言語ではありません。C ++では、std::shared_ptr標準の参照カウントスマートポインターテンプレートであるを使用するのが一般的です。Windowsコンポーネントオブジェクトモデルのプログラミングにはそれが必要です。多くの言語は、ガベージコレクション戦略として舞台裏で自動参照カウントを使用しています。

ウィキペディアの記事は、詳細情報を探し始めるのに適した場所です:http: //en.wikipedia.org/wiki/Reference_counting

于 2011-06-27T14:18:17.557 に答える