Skip to content

Commit 6b7d93d

Browse files
committed
Making the destructors of all COM objects protected so they can only be freed by calling Release().
1 parent 1e7f998 commit 6b7d93d

File tree

10 files changed

+29
-21
lines changed

10 files changed

+29
-21
lines changed

CandidateWindow.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class CandidateWindow :
3636
public ITfCandidateListUIElement {
3737
public:
3838
CandidateWindow(TextService* service, EditSession* session);
39-
~CandidateWindow(void);
4039

4140
// IUnknown
4241
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj);
@@ -111,6 +110,9 @@ class CandidateWindow :
111110
void paintItem(HDC hDC, int i, int x, int y);
112111
void itemRect(int i, RECT& rect);
113112

113+
protected: // COM object should not be deleted directly. calling Release() instead.
114+
~CandidateWindow(void);
115+
114116
private:
115117
ULONG refCount_;
116118
BOOL shown_;

DisplayAttributeInfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ namespace Ime {
2727
class DisplayAttributeInfo : public ITfDisplayAttributeInfo {
2828
public:
2929
DisplayAttributeInfo(const GUID& guid);
30-
virtual ~DisplayAttributeInfo(void);
3130

3231
// public methods
3332

@@ -103,6 +102,9 @@ class DisplayAttributeInfo : public ITfDisplayAttributeInfo {
103102
STDMETHODIMP SetAttributeInfo(const TF_DISPLAYATTRIBUTE *ptfDisplayAttr);
104103
STDMETHODIMP Reset();
105104

105+
protected: // COM object should not be deleted directly. calling Release() instead.
106+
virtual ~DisplayAttributeInfo(void);
107+
106108
private:
107109
int refCount_;
108110
TfGuidAtom atom_;

DisplayAttributeInfoEnum.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class DisplayAttributeInfoEnum : public IEnumTfDisplayAttributeInfo {
3333
public:
3434
DisplayAttributeInfoEnum(DisplayAttributeProvider* provider);
3535
DisplayAttributeInfoEnum(const DisplayAttributeInfoEnum& other);
36-
virtual ~DisplayAttributeInfoEnum(void);
3736

3837
// IUnknown
3938
STDMETHODIMP QueryInterface(REFIID riid, void **ppvObj);
@@ -46,6 +45,9 @@ class DisplayAttributeInfoEnum : public IEnumTfDisplayAttributeInfo {
4645
STDMETHODIMP Reset();
4746
STDMETHODIMP Skip(ULONG ulCount);
4847

48+
protected: // COM object should not be deleted directly. calling Release() instead.
49+
virtual ~DisplayAttributeInfoEnum(void);
50+
4951
private:
5052
int refCount_;
5153
std::list<DisplayAttributeInfo*>::iterator iterator_;

DisplayAttributeProvider.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class DisplayAttributeProvider : public ITfDisplayAttributeProvider {
3535
friend class DisplayAttributeInfoEnum;
3636

3737
DisplayAttributeProvider(ImeModule* module);
38-
virtual ~DisplayAttributeProvider(void);
3938

4039
// COM stuff
4140

@@ -48,6 +47,9 @@ class DisplayAttributeProvider : public ITfDisplayAttributeProvider {
4847
STDMETHODIMP EnumDisplayAttributeInfo(IEnumTfDisplayAttributeInfo **ppEnum);
4948
STDMETHODIMP GetDisplayAttributeInfo(REFGUID guidInfo, ITfDisplayAttributeInfo **ppInfo);
5049

50+
protected: // COM object should not be deleted directly. calling Release() instead.
51+
virtual ~DisplayAttributeProvider(void);
52+
5153
private:
5254
int refCount_;
5355
ComPtr<ImeModule> imeModule_;
@@ -56,5 +58,3 @@ class DisplayAttributeProvider : public ITfDisplayAttributeProvider {
5658
}
5759

5860
#endif
59-
60-

EditSession.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class TextService;
2929
class EditSession: public ITfEditSession {
3030
public:
3131
EditSession(TextService* service, ITfContext* context);
32-
virtual ~EditSession(void);
3332

3433
TextService* textService() {
3534
return textService_;
@@ -53,6 +52,9 @@ class EditSession: public ITfEditSession {
5352
// ITfEditSession
5453
virtual STDMETHODIMP DoEditSession(TfEditCookie ec);
5554

55+
protected: // COM object should not be deleted directly. calling Release() instead.
56+
virtual ~EditSession(void);
57+
5658
protected:
5759
TextService* textService_;
5860
ITfContext* context_;

ImeModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,8 @@ bool ImeModule::registerDisplayAttributeInfos() {
461461
return false;
462462
}
463463

464-
void ImeModule::freeTextService(TextService* service) {
464+
void ImeModule::removeTextService(TextService* service) {
465465
textServices_.remove(service);
466-
delete service;
467466
}
468467

469468
// virtual

ImeModule.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class ImeModule:
4848
public ITfFnConfigure {
4949
public:
5050
ImeModule(HMODULE module, const CLSID& textServiceClsid);
51-
virtual ~ImeModule(void);
5251

5352
// public methods
5453
HINSTANCE hInstance() const {
@@ -77,7 +76,7 @@ class ImeModule:
7776

7877
// should be override by IME implementors
7978
virtual TextService* createTextService() = 0;
80-
void freeTextService(TextService* service);
79+
void removeTextService(TextService* service);
8180

8281
// called when config dialog needs to be launched
8382
virtual bool onConfigure(HWND hwndParent, LANGID langid, REFGUID rguidProfile);
@@ -121,6 +120,9 @@ class ImeModule:
121120
// ITfFnConfigure
122121
STDMETHODIMP Show(HWND hwndParent, LANGID langid, REFGUID rguidProfile);
123122

123+
protected: // COM object should not be deleted directly. calling Release() instead.
124+
virtual ~ImeModule(void);
125+
124126
private:
125127
volatile unsigned long refCount_;
126128
HINSTANCE hInstance_;

LangBarButton.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class LangBarButton:
3434
public ITfSource {
3535
public:
3636
LangBarButton(TextService* service, const GUID& guid, UINT commandId = 0, const wchar_t* text = NULL, DWORD style = TF_LBI_STYLE_BTN_BUTTON);
37-
virtual ~LangBarButton(void);
3837

3938
// public methods
4039
const wchar_t* text() const;
@@ -95,6 +94,9 @@ class LangBarButton:
9594
return textService_;
9695
};
9796

97+
protected: // COM object should not be deleted directly. calling Release() instead.
98+
virtual ~LangBarButton(void);
99+
98100
private:
99101
void buildITfMenu(ITfMenu* menu, HMENU templ);
100102

TextService.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,12 @@ TextService::TextService(ImeModule* module):
4646
langBarSinkCookie_(TF_INVALID_COOKIE),
4747
activateLanguageProfileNotifySinkCookie_(TF_INVALID_COOKIE),
4848
composition_(NULL),
49-
candidateWindow_(NULL),
5049
refCount_(1) {
5150

5251
addCompartmentMonitor(GUID_COMPARTMENT_KEYBOARD_OPENCLOSE, false);
5352
}
5453

5554
TextService::~TextService(void) {
56-
if(candidateWindow_)
57-
delete candidateWindow_;
5855

5956
// This should only happen in rare cases
6057
if(!compartmentMonitors_.empty()) {
@@ -624,9 +621,9 @@ STDMETHODIMP_(ULONG) TextService::Release(void) {
624621
assert(refCount_ > 0);
625622
--refCount_;
626623
if(0 == refCount_) {
627-
// We do not call "delete this" since ImeModule needs to do
628-
// some clean up before deleting the TextService object.
629-
module_->freeTextService(this);
624+
// ImeModule needs to do some clean up before deleting the TextService object.
625+
module_->removeTextService(this);
626+
delete this;
630627
}
631628
return refCount_;
632629
}

TextService.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
namespace Ime {
3939

4040
class ImeModule;
41-
class CandidateWindow;
4241
class LangBarButton;
4342

4443
class TextService:
@@ -61,7 +60,6 @@ class TextService:
6160
};
6261

6362
TextService(ImeModule* module);
64-
virtual ~TextService(void);
6563

6664
// public methods
6765
ImeModule* imeModule() const;
@@ -286,6 +284,9 @@ class TextService:
286284
}
287285
};
288286

287+
protected: // COM object should not be deleted directly. calling Release() instead.
288+
virtual ~TextService(void);
289+
289290
private:
290291
ComPtr<ImeModule> module_;
291292
ComPtr<ITfThreadMgr> threadMgr_;
@@ -303,7 +304,6 @@ class TextService:
303304
DWORD activateLanguageProfileNotifySinkCookie_;
304305

305306
ITfComposition* composition_; // acquired when starting composition, released when ending composition
306-
CandidateWindow* candidateWindow_;
307307
ComPtr<ITfLangBarMgr> langBarMgr_;
308308
std::vector<LangBarButton*> langBarButtons_;
309309
std::vector<PreservedKey> preservedKeys_;

0 commit comments

Comments
 (0)