Bug 666414 - Prevent AddRef and Release from being called on the pointers wrapped in nsCOMPtr and nsRefPtr; r=bsmedberg

This commit is contained in:
Ehsan Akhgari 2011-09-16 16:22:44 -04:00
parent 98483e9eff
commit b542efd4e6
20 changed files with 63 additions and 38 deletions

View File

@ -4313,8 +4313,8 @@ nsCanvasRenderingContext2DAzure::GetThebesSurface(gfxASurface **surface)
mTarget->Flush();
}
mThebesSurface->AddRef();
*surface = mThebesSurface;
NS_ADDREF(*surface);
return NS_OK;
}

View File

@ -58,7 +58,7 @@ public:
virtual ~nsXBLDocumentInfo();
already_AddRefed<nsIDocument> GetDocument()
{ NS_ADDREF(mDocument); return mDocument.get(); }
{ nsCOMPtr<nsIDocument> copy = mDocument; return copy.forget(); }
PRBool GetScriptAccess() { return mScriptAccess; }

View File

@ -504,8 +504,8 @@ nsDummyJavaPluginOwner::SetInstance(nsNPAPIPluginInstance *aInstance)
NS_IMETHODIMP
nsDummyJavaPluginOwner::GetInstance(nsNPAPIPluginInstance **aInstance)
{
NS_IF_ADDREF(mInstance);
*aInstance = mInstance;
NS_IF_ADDREF(*aInstance);
return NS_OK;
}
@ -8963,6 +8963,8 @@ nsGlobalWindow::SetTimeoutOrInterval(nsIScriptTimeoutHandler *aHandler,
return rv;
}
nsRefPtr<nsTimeout> copy = timeout;
rv = timeout->mTimer->InitWithFuncCallback(TimerCallback, timeout,
realInterval,
nsITimer::TYPE_ONE_SHOT);
@ -8971,7 +8973,7 @@ nsGlobalWindow::SetTimeoutOrInterval(nsIScriptTimeoutHandler *aHandler,
}
// The timeout is now also held in the timer's closure.
timeout->AddRef();
copy.forget();
} else {
// If we are frozen, however, then we instead simply set
// timeout->mTimeRemaining to be the "time remaining" in the timeout (i.e.,

View File

@ -557,8 +557,8 @@ nsNPAPIPlugin::CreatePluginInstance(nsNPAPIPluginInstance **aResult)
if (!inst)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(inst);
*aResult = inst;
NS_ADDREF(*aResult);
return NS_OK;
}

View File

@ -476,8 +476,8 @@ nsresult nsPluginInstanceOwner::GetInstance(nsNPAPIPluginInstance **aInstance)
{
NS_ENSURE_ARG_POINTER(aInstance);
NS_IF_ADDREF(mInstance);
*aInstance = mInstance;
NS_IF_ADDREF(*aInstance);
return NS_OK;
}

View File

@ -140,10 +140,10 @@ nsDOMDesktopNotification::nsDOMDesktopNotification(const nsAString & title,
// Retain a reference so the object isn't deleted without IPDL's knowledge.
// Corresponding release occurs in DeallocPContentPermissionRequest.
request->AddRef();
nsRefPtr<nsDesktopNotificationRequest> copy = request;
nsCString type = NS_LITERAL_CSTRING("desktop-notification");
child->SendPContentPermissionRequestConstructor(request, type, IPC::URI(mURI));
child->SendPContentPermissionRequestConstructor(copy.forget().get(), type, IPC::URI(mURI));
request->Sendprompt();
return;

View File

@ -1717,8 +1717,8 @@ nsPlaintextEditor::SelectEntireDocument(nsISelection *aSelection)
already_AddRefed<nsIDOMEventTarget>
nsPlaintextEditor::GetDOMEventTarget()
{
NS_IF_ADDREF(mEventTarget);
return mEventTarget.get();
nsCOMPtr<nsIDOMEventTarget> copy = mEventTarget;
return copy.forget();
}

View File

@ -1554,10 +1554,8 @@ public:
virtual already_AddRefed<gfxASurface> GetBackingSurface()
{
if (mBackingSurface) {
NS_ADDREF(mBackingSurface);
}
return mBackingSurface.get();
nsRefPtr<gfxASurface> copy = mBackingSurface;
return copy.forget();
}
virtual PRBool CreateEGLSurface(gfxASurface* aSurface)

View File

@ -879,8 +879,8 @@ public:
virtual already_AddRefed<gfxASurface> GetBackingSurface()
{
NS_ADDREF(mUpdateSurface);
return mUpdateSurface.get();
nsRefPtr<gfxASurface> copy = mUpdateSurface;
return copy.forget();
}
virtual PRBool InUpdate() const { return mInUpdate; }

View File

@ -158,10 +158,8 @@ class PrefCallback : public PLDHashEntryHdr {
already_AddRefed<nsIObserver> GetObserver() const
{
if (!IsWeak()) {
NS_IF_ADDREF(mStrongRef);
// We need to call get() here because we can't convert an nsCOMPtr to
// an already_AddRefed.
return mStrongRef.get();
nsCOMPtr<nsIObserver> copy = mStrongRef;
return copy.forget();
}
nsCOMPtr<nsIObserver> observer = do_QueryReferent(mWeakRef);

View File

@ -1589,8 +1589,8 @@ nsHalfOpenSocket::OnOutputStreamReady(nsIAsyncOutputStream *out)
// minimum granularity we can expect a server to be timing out with.
conn->SetIsReusedAfter(950);
NS_ADDREF(conn); // because onmsg*() expects to drop a reference
gHttpHandler->ConnMgr()->OnMsgReclaimConnection(NS_OK, conn);
nsRefPtr<nsHttpConnection> copy(conn); // because onmsg*() expects to drop a reference
gHttpHandler->ConnMgr()->OnMsgReclaimConnection(NS_OK, conn.forget().get());
}
return rv;

View File

@ -399,7 +399,8 @@ test_observer_topic_dispatched()
// Need two Link objects as well - one for each URI.
nsCOMPtr<Link> visitedLink(new mock_Link(expect_visit, false));
NS_ADDREF(visitedLink); // It will release itself when notified.
nsCOMPtr<Link> visitedLinkCopy = visitedLink;
visitedLinkCopy.forget(); // It will release itself when notified.
nsCOMPtr<Link> notVisitedLink(new mock_Link(expect_no_visit));
// Add the right observers for the URIs to check results.

View File

@ -1425,8 +1425,8 @@ NS_IMETHODIMP nsViewManager::SetViewZIndex(nsIView *aView, PRBool aAutoZIndex, P
NS_IMETHODIMP nsViewManager::GetDeviceContext(nsDeviceContext *&aContext)
{
NS_IF_ADDREF(mContext);
aContext = mContext;
NS_IF_ADDREF(aContext);
return NS_OK;
}

View File

@ -101,8 +101,8 @@ GetRenderingContext(nsIDocShell *shell, gfxASurface *surface,
NS_WARNING("Could not create nsICanvasRenderingContext2D for tab previews!");
return rv;
}
NS_ADDREF(ctx);
gCtx = ctx;
NS_ADDREF(gCtx);
}
nsCOMPtr<nsICanvasRenderingContextInternal> ctxI = do_QueryInterface(ctx, &rv);

View File

@ -1049,11 +1049,11 @@ class nsRefPtr
return get();
}
T*
nsCOMPtr_base::nsDerivedSafe<T>*
operator->() const
{
NS_PRECONDITION(mRawPtr != 0, "You can't dereference a NULL nsRefPtr with operator->().");
return get();
return reinterpret_cast<nsCOMPtr_base::nsDerivedSafe<T>*> (get());
}
// This operator is needed for gcc <= 4.0.* and for Sun Studio; it

View File

@ -117,7 +117,7 @@ Omnijar::InitOne(nsIFile *aPath, Type aType)
CleanUpOne(aType);
sReader[aType] = zipReader;
sPath[aType] = file;
NS_IF_ADDREF(file);
NS_IF_ADDREF(sPath[aType]);
}
void

View File

@ -1987,8 +1987,9 @@ nsFactoryEntry::GetFactory()
if (!mFactory)
return NULL;
}
NS_ADDREF(mFactory);
return mFactory.get();
nsIFactory* factory = mFactory.get();
NS_ADDREF(factory);
return factory;
}
////////////////////////////////////////////////////////////////////////////////

View File

@ -429,6 +429,31 @@ nsCOMPtr_base
{
public:
template <class T>
class
NS_FINAL_CLASS
NS_STACK_CLASS
nsDerivedSafe : public T
/*
No client should ever see or have to type the name of this class. It is the
artifact that makes it a compile-time error to call |AddRef| and |Release|
on a |nsCOMPtr|. DO NOT USE THIS TYPE DIRECTLY IN YOUR CODE.
See |nsCOMPtr::operator->| and |nsRefPtr::operator->|.
*/
{
private:
using T::AddRef;
using T::Release;
protected:
nsDerivedSafe(); // NOT TO BE IMPLEMENTED
/*
This ctor exists to avoid compile errors and warnings about nsDerivedSafe using the
default ctor but inheriting classes without an empty ctor. See bug 209667.
*/
};
nsCOMPtr_base( nsISupports* rawPtr = 0 )
: mRawPtr(rawPtr)
{
@ -803,11 +828,11 @@ nsCOMPtr
return get();
}
T*
nsCOMPtr_base::nsDerivedSafe<T>*
operator->() const
{
NS_PRECONDITION(mRawPtr != 0, "You can't dereference a NULL nsCOMPtr with operator->().");
return get();
return reinterpret_cast<nsCOMPtr_base::nsDerivedSafe<T>*> (get());
}
nsCOMPtr<T>*
@ -1110,11 +1135,11 @@ class nsCOMPtr<nsISupports>
return get();
}
nsISupports*
nsDerivedSafe<nsISupports>*
operator->() const
{
NS_PRECONDITION(mRawPtr != 0, "You can't dereference a NULL nsCOMPtr with operator->().");
return get();
return reinterpret_cast<nsCOMPtr_base::nsDerivedSafe<nsISupports>*> (get());
}
nsCOMPtr<nsISupports>*

View File

@ -149,8 +149,8 @@ nsInterfaceHashtable<KeyClass,Interface>::Get(KeyType aKey) const
if (!ent)
return NULL;
NS_IF_ADDREF(ent->mData);
return already_AddRefed<Interface>(ent->mData);
nsCOMPtr<Interface> copy = ent->mData;
return copy.forget();
}
template<class KeyClass,class Interface>

View File

@ -220,8 +220,8 @@ class ScopedXPCOM : public nsIDirectoryServiceProvider2
already_AddRefed<nsIFile> GetProfileDirectory()
{
if (mProfD) {
NS_ADDREF(mProfD);
return mProfD.get();
nsCOMPtr<nsIFile> copy = mProfD;
return copy.forget();
}
// Create a unique temporary folder to use for this test.