Bug 995893 part.1 Use nsAutoRefCnt in IUnknown inherited classes and log the refcounting r=jimm

This commit is contained in:
Masayuki Nakano 2014-07-12 02:09:59 +09:00
parent 7c9c788494
commit 4cae0ee102
3 changed files with 45 additions and 38 deletions

View File

@ -26,6 +26,43 @@
#include "mozilla/Attributes.h"
/**
* NS_INLINE_DECL_IUNKNOWN_REFCOUNTING should be used for defining and
* implementing AddRef() and Release() of IUnknown interface.
* This depends on xpcom/glue/nsISupportsImpl.h.
*/
#define NS_INLINE_DECL_IUNKNOWN_REFCOUNTING(_class) \
public: \
STDMETHODIMP_(ULONG) AddRef() \
{ \
MOZ_ASSERT_TYPE_OK_FOR_REFCOUNTING(_class) \
MOZ_ASSERT(int32_t(mRefCnt) >= 0, "illegal refcnt"); \
NS_ASSERT_OWNINGTHREAD(_class); \
++mRefCnt; \
NS_LOG_ADDREF(this, mRefCnt, #_class, sizeof(*this)); \
return static_cast<ULONG>(mRefCnt.get()); \
} \
STDMETHODIMP_(ULONG) Release() \
{ \
MOZ_ASSERT(int32_t(mRefCnt) > 0, \
"Release called on object that has already been released!"); \
NS_ASSERT_OWNINGTHREAD(_class); \
--mRefCnt; \
NS_LOG_RELEASE(this, mRefCnt, #_class); \
if (mRefCnt == 0) { \
NS_ASSERT_OWNINGTHREAD(_class); \
mRefCnt = 1; /* stabilize */ \
delete this; \
return 0; \
} \
return static_cast<ULONG>(mRefCnt.get()); \
} \
protected: \
nsAutoRefCnt mRefCnt; \
NS_DECL_OWNINGTHREAD \
public:
class nsWindow;
class nsWindowBase;
struct KeyPair;

View File

@ -61,27 +61,14 @@ PRLogModuleInfo* sTextStoreLog = nullptr;
class InputScopeImpl MOZ_FINAL : public ITfInputScope
{
public:
InputScopeImpl(const nsTArray<InputScope>& aList) :
mRefCnt(1),
mInputScopes(aList)
InputScopeImpl(const nsTArray<InputScope>& aList)
: mInputScopes(aList)
{
PR_LOG(sTextStoreLog, PR_LOG_ALWAYS,
("TSF: 0x%p InputScopeImpl()", this));
}
STDMETHODIMP_(ULONG) AddRef(void) { return ++mRefCnt; }
STDMETHODIMP_(ULONG) Release(void)
{
--mRefCnt;
if (mRefCnt) {
return mRefCnt;
}
PR_LOG(sTextStoreLog, PR_LOG_ALWAYS,
("TSF: 0x%p InputScopeImpl::Release() final", this));
delete this;
return 0;
}
NS_INLINE_DECL_IUNKNOWN_REFCOUNTING(InputScopeImpl)
STDMETHODIMP QueryInterface(REFIID riid, void** ppv)
{
@ -127,7 +114,6 @@ public:
STDMETHODIMP GetXML(BSTR *pbstrXML) { return E_NOTIMPL; }
private:
DWORD mRefCnt;
nsTArray<InputScope> mInputScopes;
};
@ -519,7 +505,6 @@ GetDisplayAttrStr(const TF_DISPLAYATTRIBUTE &aDispAttr)
nsTextStore::nsTextStore()
: mContent(mComposition, mSelection)
{
mRefCnt = 1;
mEditCookie = 0;
mIPProfileCookie = TF_INVALID_COOKIE;
mLangProfileCookie = TF_INVALID_COOKIE;
@ -761,20 +746,6 @@ nsTextStore::QueryInterface(REFIID riid,
return E_NOINTERFACE;
}
STDMETHODIMP_(ULONG) nsTextStore::AddRef()
{
return ++mRefCnt;
}
STDMETHODIMP_(ULONG) nsTextStore::Release()
{
--mRefCnt;
if (0 != mRefCnt)
return mRefCnt;
delete this;
return 0;
}
STDMETHODIMP
nsTextStore::AdviseSink(REFIID riid,
IUnknown *punk,
@ -2252,7 +2223,8 @@ nsTextStore::RetrieveRequestedAttrs(ULONG ulCount,
if (mInputScopeRequested) {
paAttrVals->varValue.vt = VT_UNKNOWN;
paAttrVals->varValue.punkVal = (IUnknown*) new InputScopeImpl(mInputScopes);
nsRefPtr<IUnknown> inputScope = new InputScopeImpl(mInputScopes);
paAttrVals->varValue.punkVal = inputScope.forget().take();
}
mInputScopeDetected = mInputScopeRequested = false;

View File

@ -11,6 +11,7 @@
#include "nsCOMPtr.h"
#include "nsIWidget.h"
#include "nsWindowBase.h"
#include "WinUtils.h"
#include "mozilla/Attributes.h"
#include "mozilla/TextRange.h"
#include "mozilla/WindowsVersion.h"
@ -53,9 +54,9 @@ class nsTextStore MOZ_FINAL : public ITextStoreACP,
public ITfInputProcessorProfileActivationSink
{
public: /*IUnknown*/
STDMETHODIMP_(ULONG) AddRef(void);
STDMETHODIMP QueryInterface(REFIID, void**);
STDMETHODIMP_(ULONG) Release(void);
NS_INLINE_DECL_IUNKNOWN_REFCOUNTING(nsTextStore)
public: /*ITextStoreACP*/
STDMETHODIMP AdviseSink(REFIID, IUnknown*, DWORD);
@ -696,9 +697,6 @@ protected:
// Message the Tablet Input Panel uses to flush text during blurring.
// See comments in Destroy
static UINT sFlushTIPInputMessage;
private:
ULONG mRefCnt;
};
#endif /*NSTEXTSTORE_H_*/