The default bug view has changed. See this FAQ.

XPCOM QueryInterface appears to be implemented wrongly in Weblock tutorial

RESOLVED FIXED

Status

Developer Documentation
General
RESOLVED FIXED
13 years ago
4 years ago

People

(Reporter: Chris, Assigned: Dawn Endico)

Tracking

Details

(URL)

(Reporter)

Description

13 years ago
User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.1) Gecko/20031114
Build Identifier: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4.1) Gecko/20031114

The weblock tutorial at
http://www.mozilla.org/projects/xpcom/book/cxc/html/quicktour2.html#1003494 
gives the following code as an example of how to implement a QueryInterface
function:

class Sample: public nsISupports
{
private:
    nsrefcnt mRefCnt;

public:
    Sample();
    virtual ~Sample();

    NS_IMETHOD QueryInterface(const nsIID &aIID, void **aResult);
    NS_IMETHOD_(nsrefcnt) AddRef(void);
    NS_IMETHOD_(nsrefcnt) Release(void);

};

Sample::Sample()
{
    mRefCnt(0);
}

Sample::~Sample()
{
}

NS_IMETHODIMP Sample::QueryInterface(const nsIID &aIID,
                     void **aResult)
{
    if (aResult == NULL)
    {
        return NS_ERROR_NULL_POINTER;
    }

    *aResult = NULL;

    if (aIID.Equals(kISupportsIID))
    {
        *aResult = (void *) this;
    }

    if (aResult != NULL)
    {
        return NS_ERROR_NO_INTERFACE;
    }

    AddRef();
    return NS_OK;
}

NS_IMETHODIMP_(nsrefcnt) Sample::AddRef()
{
    return ++mRefCnt;
}

NS_IMETHODIMP_(nsrefcnt) Sample::Release()
{
    if (--mRefCnt == 0)
    {
        delete this;
        return 0;
    }

    return mRefCnt;
}

I'm a bit confused about the code in QueryInterface:

    if (aResult != NULL)
    {
        return NS_ERROR_NO_INTERFACE;
    }


Shouldn't aResult contain a reference to an interface? Why isn't this
code something like "if (aResult == NULL)" ??

Why does this function test that aResult is NOT equal to NULL? If the result is
a non-NULL value, wouldn't this mean that aResult has accessed an interface?

Somewhat confused.

Incidently, the XPCOM reference implementation of QueryInterface at
http://www.mozilla.org/projects/xpcom/QI.html might be a better way to go in the
tutorial. From what I can see, aIID is tested via the equals function, and if it
can't be found they place the zero into a temporary nsISupports pointer called
foundInterface, if it is found then the address of the interface is placed into
this variable. They then test the ptr with the following:

    nsresult status;
    if ( !foundInterface )
      status = NS_NOINTERFACE;
    else
      {
        NS_ADDREF(foundInterface);
        status = NS_OK;
      }

Reproducible: Always

Steps to Reproduce:
Documentation issue.

Comment 1

10 years ago
Fixed in the MDC version:
http://developer.mozilla.org/en/docs/Creating_XPCOM_Components:An_Overview_of_XPCOM#Object_Ownership
Status: NEW → RESOLVED
Last Resolved: 10 years ago
Resolution: --- → FIXED
Component: Mozilla Developer → Documentation Requests
Product: Documentation → Mozilla Developer Center
Component: Documentation Requests → Documentation
Product: Mozilla Developer Network → Mozilla Developer Network
Component: Documentation → General
Product: Mozilla Developer Network → Developer Documentation
You need to log in before you can comment on or make changes to this bug.