Sign in to follow this  
ratha

Problem with CreateMutex

Recommended Posts

ratha    100
Hi!

I've developed a Dialog based MFC application. I've created a Mutex in this application to run only one instance at any time. I've used the

same code to create another application(I've changed only resources like Logos, Images and Flash movies). I've used the following code to

create mutex:

[code] if(NULL != ::CreateMutex(NULL, TRUE,_T("{AFD2966D-9A83-4E3A-9C9E-CD81E96D819A}")))
{
long dwError = ::GetLastError();
if(dwError == ERROR_ALREADY_EXISTS)
// MessageBox(NULL,L"You are Already Running Application.",L"ERROR!!",MB_OK);
EndDialog(NULL,IDOK);
}[/code]

for the first application. My problem is:

If I run application1 and application application2 simultaneously, they run fine. But if I close application1, application2 is also closed. Similarly if I close application2, application1 is also closed. Which I don't want. How to fix this issue? I'm sure that I've used adifferent GUID value to create Mutex for the second application.

Share this post


Link to post
Share on other sites
ratha    100
[quote name='smasherprog' timestamp='1306856945' post='4817909']
set the second parameter to false
[/quote]

I've already tried this. But I got the same result. If I close app1 or app2, the other one is automatically get colsed.

Share this post


Link to post
Share on other sites
ApochPiQ    23005
You should be checking if CreateMutex() returns NULL to see if it had an error; a non-NULL result indicates that the mutex was created successfully, so your code has a bug.
In any case, this is not a behaviour that you can blame entirely on the mutex; closing a mutex does not affect other processes. You'll need to do some investigation as to [i]why[/i] your other application instance is getting closed. For example, is it crashing silently? Does attaching a debugger help? Can you watch the app's messages using Spy++ or a similar tool to see if something like WM_QUIT is posted? Can you add any logging features to the code to help determine what is going on when it exits?

Share this post


Link to post
Share on other sites
ratha    100
[quote name='ApochPiQ' timestamp='1306909431' post='4818170']
You should be checking if CreateMutex() returns NULL to see if it had an error; a non-NULL result indicates that the mutex was created successfully, so your code has a bug.
In any case, this is not a behaviour that you can blame entirely on the mutex; closing a mutex does not affect other processes. You'll need to do some investigation as to [i]why[/i] your other application instance is getting closed. For example, is it crashing silently? Does attaching a debugger help? Can you watch the app's messages using Spy++ or a similar tool to see if something like WM_QUIT is posted? Can you add any logging features to the code to help determine what is going on when it exits?
[/quote]

I've used the same code, same project name and etc. (except GUID). I've used different GUID different apps. I've used the following code to terminate thread:

[code]
TerminateThread(Retry::ThreadExec, 0);
TerminateThread(TransferringForm::ThreadExec, 0) ;
TerminateThread(TransferringForm::ThreadExec1, 0) ;
[/code]

Is this code a cause of this problem?

Share this post


Link to post
Share on other sites
the_edd    2109
[quote name='ratha' timestamp='1306911104' post='4818177']
I've used the same code, same project name and etc. (except GUID). I've used different GUID different apps.
[/quote]
Guessing won't get you too far when using the C++. It's a brutal and unforgiving language. First try and understand what a Windows Mutex is and then attempt to implement your solution.

Here is some example code.

[code]
#include <windows.h>
#include <stdexcept>
#include <string>

class application_beheaded : public std::runtime_error
{
public:
application_beheaded() : std::runtime_error("pop") { }
};

class there_can_be_only_one
{
public:
there_can_be_only_one(const std::string &unique_name) :
mutex(CreateMutexA(NULL, TRUE, unique_name.c_str()))
{
if (!mutex)
throw std::bad_alloc(); // I guess :/

if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(mutex);
throw application_beheaded();
}
}

~there_can_be_only_one() { CloseHandle(mutex); }

private:
there_can_be_only_one(const there_can_be_only_one &);
there_can_be_only_one &operator=(const there_can_be_only_one &);

private:
HANDLE mutex;
};

int main()
{
try
{
there_can_be_only_one highlander("Duncan MacLeod");
MessageBoxA(NULL, "I am immortal", "Yeah!", MB_OK);
}
catch (const application_beheaded &ex)
{
MessageBoxA(NULL, "Oops", ex.what(), MB_OK);
}

return 0;
}
[/code]


[quote]
I've used the following code to terminate thread:

[code]
TerminateThread(Retry::ThreadExec, 0);
TerminateThread(TransferringForm::ThreadExec, 0) ;
TerminateThread(TransferringForm::ThreadExec1, 0) ;
[/code]

Is this code a cause of this problem?
[/quote]
You haven't really provided enough information to anyone to get a handle on the specifics of what's going on in your code. However, in my opinion, calls to TerminateThread should be avoided wherever possible as they don't allow the thread to finish cleanly, so a log file may contain half a written line, memory may be leaked, mutexes may remain locked, and so on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this