Skip to content

Commit 407090d

Browse files
committed
fix: clean up mutex lifecycle and remove unnecessary ReleaseMutex call
- Clear m_hMutex after CloseHandle in destructor to avoid dangling handle - Null out mutex handle when existing instance is detected in InstanceRunning() - Remove redundant ReleaseMutex() call in CloseApplication() - Use TRUE explicitly for initial mutex ownership
1 parent d124e5c commit 407090d

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

ColorCop.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ CColorCopApp::CColorCopApp() {
3939
CColorCopApp::~CColorCopApp() {
4040
if (m_hMutex != nullptr) {
4141
CloseHandle(m_hMutex);
42+
m_hMutex = nullptr;
4243
}
4344
}
4445

@@ -76,11 +77,12 @@ BOOL CColorCopApp::InitInstance() {
7677

7778
// uses a Mutex to figure out if there is an instance of color cop running
7879
bool CColorCopApp::InstanceRunning() {
79-
m_hMutex = CreateMutex(NULL, true, _T("ColorCop_Mutex"));
80+
m_hMutex = CreateMutex(NULL, TRUE, _T("ColorCop_Mutex"));
8081

8182
if (m_hMutex) {
8283
if (GetLastError() == ERROR_ALREADY_EXISTS) {
8384
CloseHandle(m_hMutex); // Close the duplicate handle
85+
m_hMutex = nullptr;
8486
return true;
8587
}
8688
return false;
@@ -212,10 +214,6 @@ void CColorCopApp::CloseApplication() {
212214
CArchive ar(&file, CArchive::store);
213215
Serialize(ar);
214216
}
215-
216-
ReleaseMutex(m_hMutex);
217-
218-
return;
219217
}
220218

221219
void CColorCopApp::Serialize(CArchive& ar) {

0 commit comments

Comments
 (0)