CComPtr 클래스

COM 인터페이스 포인터를 관리하기 위한 스마트 포인터 클래스입니다.

구문

template<class T>
class CComPtr

매개 변수

T
저장할 포인터의 형식을 지정하는 COM 인터페이스입니다.

멤버

공용 생성자

속성 설명
CComPtr::CComPtr 생성자입니다.

Public 연산자

이름 설명
CComPtr::operator = 멤버 포인터에 포인터를 할당합니다.

설명

ATL은 COM 인터페이스 포인터를 사용하고 CComPtrCComQIPtr 관리합니다. 둘 다 파생 CComPtrBase되며 둘 다 자동 참조 계산을 수행합니다.

CComQIPtr 클래스는 CComPtr 자동 참조 계산을 수행하여 메모리 누수를 제거하는 데 도움이 될 수 있습니다. 다음 함수는 모두 동일한 논리 작업을 수행합니다. 그러나 두 번째 버전은 클래스를 사용하기 CComPtr 때문에 오류가 덜 발생할 수 있습니다.

// Error-checking routine that performs manual lifetime management
// of a COM IErrorInfo object
HRESULT CheckComError_Manual()
{
   HRESULT hr;
   CComBSTR bstrDescription; 
   CComBSTR bstrSource; 
   CComBSTR bstrHelpFile; 

   IErrorInfo* pErrInfo = NULL; // naked COM interface pointer
   hr = ::GetErrorInfo(0, &pErrInfo);
   if(hr != S_OK)
      return hr;

   hr = pErrInfo->GetDescription(&bstrDescription); 
   if(FAILED(hr))
   {
      pErrInfo->Release();   // must release interface pointer before returning
      return hr;
   }

   hr = pErrInfo->GetSource(&bstrSource);
   if(FAILED(hr))
   {
      pErrInfo->Release();   // must release interface pointer before returning
      return hr;
   }

   hr = pErrInfo->GetHelpFile(&bstrHelpFile);
   if(FAILED(hr))
   {
      pErrInfo->Release();   // must release interface pointer before returning
      return hr;
   }

   pErrInfo->Release();      // must release interface pointer before returning
   return S_OK;
}

 

// Error-checking routine that performs automatic lifetime management
// of a COM IErrorInfo object through a CComPtr smart pointer object
HRESULT CheckComError_SmartPtr()
{
   HRESULT hr;
   CComBSTR bstrDescription; 
   CComBSTR bstrSource; 
   CComBSTR bstrHelpFile; 

   CComPtr<IErrorInfo> pErrInfo; 
   hr = ::GetErrorInfo(0, &pErrInfo);
   if(hr != S_OK)
      return hr;

   hr = pErrInfo->GetDescription(&bstrDescription); 
   if(FAILED(hr))
      return hr;

   hr = pErrInfo->GetSource(&bstrSource);
   if(FAILED(hr))
      return hr;

   hr = pErrInfo->GetHelpFile(&bstrHelpFile);
   if(FAILED(hr))
      return hr;

   return S_OK;
}   // CComPtr will auto-release underlying IErrorInfo interface pointer as needed

디버그 빌드에서 코드 추적을 위해 atlsd.lib를 연결합니다.

상속 계층 구조

CComPtrBase

CComPtr

요구 사항

헤더atlbase.h:

CComPtr::CComPtr

생성자입니다.

CComPtr() throw ();
CComPtr(T* lp) throw ();
CComPtr (const CComPtr<T>& lp) throw ();

매개 변수

lp
인터페이스 포인터를 초기화하는 데 사용됩니다.

T
COM 인터페이스입니다.

설명

null 포인터가 아닌 경우 인수 호출 AddReflp을 수행하는 생성자입니다. null이 아닌 소유 개체는 CComPtr 개체의 소멸 또는 새 개체가 CComPtr 개체에 할당된 경우 호출을 받 Release 습니다.

CComPtr::operator =

대입 연산자입니다.

T* operator= (T* lp) throw ();
T* operator= (const CComPtr<T>& lp) throw ();

Return Value

업데이트 CComPtr 된 개체에 대한 포인터를 반환합니다.

설명

이 작업은 새 개체를 AddRefs하고 기존 개체가 있는 경우 해제합니다.

참고 항목

CComPtr::CComPtr
CComQIPtr::CComQIPtr
클래스 개요