I have a question on nsComptr
In the nsCOMptr document
in comparision 1
when we are using raw interface pointers the following is shown.
// raw [XP]COM interface pointers...
// given: |nsIFoo* mFooPtr;|
/*
|AddRef| the new value if it's not
|NULL|; assign it in; and |Release|
the old value, if any (so we don't
leak it).
This order of assignment is special
and must be used to avoid particular
ownership bugs.
*/
NS_IF_ADDREF(aFooPtr);
nsIFoo* temp = mFooPtr;
mFooPtr = aFooPtr;
NS_IF_RELEASE(temp);
But what if we do like this:
NS_IF_ADDREF(aFooPtr); // increments the reference count of the object
pointed by aFooPtr.
NS_IF_RELEASE(mFooPtr); // decrements the count of the object pointed to by
mFooptr and if the count is 0 delete the object
mFooPtr=aFooptr; //and then make mFooptr point to the new object which has
already been addreffed above
Is this wrong and if yes then why is it wrong?? at presnt i feel that both
ways are same.
Thanks in advance
Regards
Pras