翻訳への提案を行います
 
他のユーザーによる提案:

progress indicator
他の提案はありません。
クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
Visual Studio 2010
Visual Studio
Visual Studio の言語
Visual C++
Visual C++ のリファレンス
ATL
ATL の概念
COM および ATL の概要
COM の概要
 参照カウント
すべて縮小/すべて展開 すべて縮小
コンテンツの表示:   英語と日本語を並べて表示コンテンツの表示: 英語と日本語を並べて表示
Visual Studio 2010 - Visual C++
Reference Counting

COM itself does not automatically try to remove an object from memory when it thinks the object is no longer being used. Instead, the object programmer must remove the unused object. The programmer determines whether an object can be removed based on a reference count.

COM uses the IUnknown methods, AddRef and Release, to manage the reference count of interfaces on an object. The general rules for calling these methods are:

  • Whenever a client receives an interface pointer, AddRef must be called on the interface.

  • Whenever the client has finished using the interface pointer, it must call Release.

In a simple implementation, each AddRef call increments and each Release call decrements a counter variable inside the object. When the count returns to zero, the interface no longer has any users and is free to remove itself from memory.

Reference counting can also be implemented so that each reference to the object (not to an individual interface) is counted. In this case, each AddRef and Release call delegates to a central implementation on the object, and Release frees the entire object when its reference count reaches zero.

NoteNote

When a CComObject-derived object is constructed using the new operator, the reference count is 0. Therefore, a call to AddRef must be made after successfully creating the CComObject-derived object.

Visual Studio 2010 - Visual C++
参照カウント

COM 自身は、オブジェクトが不要になったと判断しても、メモリから自動的にオブジェクトを削除するわけではありません。 不要なオブジェクトの削除は、オブジェクトのプログラマが行います。 プログラマは、参照カウントに基づいて、オブジェクトを削除できるかどうかを判断します。

COM は、IUnknownAddRef メソッドと Release メソッドを使用して、オブジェクトのインターフェイスの参照カウントを管理します。 これらのメソッドを呼び出す一般的な規則は次のとおりです。

  • クライアントは、インターフェイス ポインターを受け取るたびに、そのインターフェイスの AddRef を呼び出す必要があります。

  • クライアントは、インターフェイス ポインターの使用を終えるたびに、Release を呼び出す必要があります。

単純な実装では、AddRef を呼び出すたびにオブジェクト内部のカウンター変数が増え、Release を呼び出すたびにカウンター変数が減ります。 カウントが 0 に戻ると、インターフェイスのユーザーはいないため、インターフェイスはメモリから自分自身を削除します。

個別のインターフェイスへの参照でなく、オブジェクトへの参照をカウントできるように、参照カウントを実装することもできます。 この場合、AddRef または Release を呼び出すと、オブジェクトの中心的な実装が代わりに呼び出されます。また、Release を呼び出すと、参照カウントが 0 に達したときオブジェクト全体が解放されます。

メモメモ

new 演算子を使用して CComObject からオブジェクトを派生したとき、参照カウントは 0 になります。 そのため、CComObject から派生したオブジェクトを作成した後で、AddRef を呼び出す必要があります。

コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2012 Microsoft. All rights reserved. 使用条件 | 商標 | プライバシー
Page view tracker