سؤال

I have some smart pointers declared as member variables inside a class in the destructor of the class , while freeing this interfaces, which code is better:

member varible

CComQIPtr<IMyInterFace> m_pMyInterface;

constructor

m_pMyInterface.CreateInstace(CLSID_MyInterface);

in Destructor

if(m_pMyInterface)
    m_pMyInterface.Release();

or should this be

if(m_pMyInterface.p ! = NULL)
    m_pMyInterface.Release();

Form the above which one is better and is there any flaw in the way i am using the interface pointers. regards tom

هل كانت مفيدة؟

المحلول

~CComQIPtr destructor takes care of everything. You don't need to release explicitly. If you however want to, e.g. to reuse the variable, you can do either of the two:

  • m_pMyInterface.Release();
  • m_pMyInterface = NULL; (operator = works with CComPtr, might be ambiguous with CComQIPtr)

NULL checking is not necessary, CComQIPtr::Release method does it anyway.

نصائح أخرى

They are equivalent, though I really don't like someone writing a book just for the sake of it.
Also, the Release() call is unneeded, just let the destructor handle it.

Go for the short one.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top