Revisión | 6c5924fd837243d5ae4a18873e42d6b50bc1e8ae (tree) |
---|---|
Tiempo | 2014-09-20 20:38:11 |
Autor | Kazuhiro Fujieda <fujieda@user...> |
Commiter | Kazuhiro Fujieda |
Make some modifications to TSFHandler
- Remove unnecessary activation of TSF
- Fix a possible null dereference
- Uninstall an installed advice sink to avoid a resource leak
@@ -15,17 +15,15 @@ TSFHandler::TSFHandler() | ||
15 | 15 | { |
16 | 16 | m_RefCount = 1; |
17 | 17 | m_ThreadMgr = nullptr; |
18 | - m_ClientId = 0; | |
18 | + m_Cookie = TF_INVALID_COOKIE; | |
19 | 19 | m_Context = nullptr; |
20 | 20 | m_CompositionState = false; |
21 | 21 | } |
22 | 22 | |
23 | 23 | TSFHandler::~TSFHandler() |
24 | 24 | { |
25 | - if (m_ThreadMgr) { | |
26 | - m_ThreadMgr->Deactivate(); | |
25 | + if (m_ThreadMgr) | |
27 | 26 | m_ThreadMgr->Release(); |
28 | - } | |
29 | 27 | if (m_Context) |
30 | 28 | m_Context->Release(); |
31 | 29 | } |
@@ -50,16 +48,13 @@ void TSFHandler::InitSink() | ||
50 | 48 | return; |
51 | 49 | } |
52 | 50 | tsfh->m_ThreadMgr = thread; |
53 | - if (FAILED(thread->Activate(&tsfh->m_ClientId))) { | |
54 | - DebugLog(_T("ThreadMgr->Activate failed.")); | |
55 | - goto fail; | |
56 | - } | |
57 | 51 | ITfSource *src; |
58 | 52 | if (FAILED(thread->QueryInterface(&src))) { |
59 | 53 | DebugLog(_T("ThreadMgr->QueryInterface failed.")); |
60 | 54 | goto fail; |
61 | 55 | } |
62 | - if (FAILED(src->AdviseSink(IID_ITfThreadMgrEventSink, static_cast<ITfThreadMgrEventSink *>(tsfh), &tsfh->m_ClientId))) { | |
56 | + DWORD cookie; | |
57 | + if (FAILED(src->AdviseSink(IID_ITfThreadMgrEventSink, static_cast<ITfThreadMgrEventSink *>(tsfh), &cookie))) { | |
63 | 58 | DebugLog(_T("Souece->AdviseSink failed.")); |
64 | 59 | src->Release(); |
65 | 60 | goto fail; |
@@ -122,20 +117,37 @@ STDMETHODIMP TSFHandler::OnSetFocus(ITfDocumentMgr *docMgr, ITfDocumentMgr *) | ||
122 | 117 | DebugLog(_T("OnSetFocus")); |
123 | 118 | if (docMgr == nullptr) |
124 | 119 | return S_OK; |
120 | + if (m_Cookie != TF_INVALID_COOKIE) { | |
121 | + ITfSource *src; | |
122 | + if (FAILED(m_Context->QueryInterface(&src))) { | |
123 | + DebugLog(_T("Context->QueryInterface:0 failed.")); | |
124 | + return S_OK; | |
125 | + } | |
126 | + HRESULT hr = src->UnadviseSink(m_Cookie); | |
127 | + src->Release(); | |
128 | + if (FAILED(hr)) { | |
129 | + DebugLog(_T("Source->UnadviceThink failed.")); | |
130 | + return S_OK; | |
131 | + } | |
132 | + m_Context->Release(); | |
133 | + m_Context = nullptr; | |
134 | + m_Cookie = TF_INVALID_COOKIE; | |
135 | + } | |
125 | 136 | ITfContext *cxt; |
126 | 137 | if (FAILED(docMgr->GetTop(&cxt))) { |
127 | 138 | DebugLog(_T("DocumentMgr->GetTop failed.")); |
128 | 139 | return S_OK; |
129 | 140 | } |
130 | - if (m_Context == cxt) | |
131 | - goto fail; | |
132 | - ITfSource *src = nullptr; | |
141 | + if (cxt == nullptr) { | |
142 | + DebugLog(_T("ITfContext is null.")); | |
143 | + return S_OK; | |
144 | + } | |
145 | + ITfSource *src; | |
133 | 146 | if (FAILED(cxt->QueryInterface(&src))) { |
134 | - DebugLog(_T("Context->QueryInterface(ITfSource) failed.")); | |
147 | + DebugLog(_T("Context->QueryInterface:1 failed.")); | |
135 | 148 | goto fail; |
136 | 149 | } |
137 | - DWORD cookie; | |
138 | - if (FAILED(src->AdviseSink(IID_ITfTextEditSink, static_cast<ITfTextEditSink *>(this), &cookie))) { | |
150 | + if (FAILED(src->AdviseSink(IID_ITfTextEditSink, static_cast<ITfTextEditSink *>(this), &m_Cookie))) { | |
139 | 151 | DebugLog(_T("Source->AdviseSink(ITfTextEditSink) failed.")); |
140 | 152 | src->Release(); |
141 | 153 | goto fail; |
@@ -28,7 +28,7 @@ public: | ||
28 | 28 | private: |
29 | 29 | ULONG m_RefCount; |
30 | 30 | ITfThreadMgr *m_ThreadMgr; |
31 | - TfClientId m_ClientId; | |
31 | + DWORD m_Cookie; | |
32 | 32 | ITfContext *m_Context; |
33 | 33 | bool m_CompositionState; |
34 | 34 | }; |