locked
InternalRelease of ComPtr tries to release an already released pointer

    Question

  • Trying some simple scenario using WRL I discovered what might be a bug of ComPtr. However it might 
    as well be a wrong usage of it from myslef.

    A C0000005 exception is thrown by ComPtr implementation in a scenario that so far doesn't look wrong at first sight.

    class CitizenClass: public RuntimeClass<ICitizen, IPerson, ISaveable>, protected SaveableHelper
    {
    InspectableClass(L"WRLCompV1.CitizenClass", BaseTrust)

    private:
    std::wstring m_name;
    std::wstring m_surname;

    ComPtr<ABI::WRLCompV1::IAddress> m_pAddress;

    public:
    CitizenClass()
    {
    ComPtr<ABI::WRLCompV1::IAddress> pAddress;
    HStringReference activableClassId(L"WRLCompV1.AddressClass");

    HRESULT hr = ActivateInstance<ComPtr<ABI::WRLCompV1::IAddress>>(activableClassId.Get(), &m_pAddress);
    }

    The IAddress object is declared using a ComPtr as an instance member of the implementtation class of
    ICitizen.

    The code is throwing the exception when all ComPtr are releasing their pointers. It happens that the IAddress pointer is released while it has already been released. The exception is thrown in the method InternalRelease().
    Using the method get_Address of ICitizen enables it.

    The implementation of this method is as follows:

    STDMETHODIMP CitizenClass::get_Address(ABI::WRLCompV1::IAddress** value)
    {
    HRESULT hr = E_POINTER;

    if (value != nullptr)
    {
    *value = m_pAddress.Get();
    hr = S_OK;
    }

    return hr;
    }

    So far I don't see anything wrong with that implementation. I used that type of construction with standard COM and it has always wotked fine. 

    I tried an implementation of the same code using directly the object interface, without wrapping it with 
    a ComPtr. Following the principles of using a COM interface I manually managed the calls to AddRed() and Release() and the same code works fine even in more complex scenario. 

    So far I have discovered other issues with the compilers that I already reported, this why I'm having serious doubts about the ComPtr class. Few years ago I also discovered a bug in the early implementation of CComPtr class of ATL, 
    bug that we corrected in the .h as it is a template class. Could it be another similar issue?

    I can provide a simple source code that demonstrate the issue and the working code.

    Thanks for your help
    O. Rouit

     

    Software Architect, Advanced IT Tokens Team - Gemalto

    Tuesday, January 1, 2013 1:01 AM

All replies