Mutex

delphi package - automated exception handling
Post Reply
emailaya
Posts: 249
Joined: Thu Oct 13, 2005 11:33 am
Contact:

Mutex

Post by emailaya »

My code is this:

Code: Select all

Mutex := CreateMutex(nil, True, 'app');  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  try
    if (Application.ExeName = 'app.exe') and ((Mutex = 0) or (GetLastError = ERROR_ALREADY_EXISTS)) then
      ShowMessage ('app is already running')
    else
    begin
      Application.Initialize;
      Application.MainFormOnTaskbar := True;
      Application.CreateForm(TfrmMain, frmMain);
      Application.Run;
    end;
  finally
    ReleaseMutex(Mutex);  <<<<<<<<<<<<<<<<<<<<<<<<
  end;
As you can see, I create the mutex and release it (a breakpoint shows the releasing is done when closing the application), still madexcept gives me the following report:

Code: Select all

allocation number: 35332
program up time: 743 ms
type: Mutex Handle
handle: $99c
access rights: $1f0001
name: \Sessions\1\BaseNamedObjects\app

main thread ($a64):
671ccc16 madExcept32.dll madExceptDbg    3538 CreateMutexWCallback
00416df2 app.exe    Winapi.Windows 35136 CreateMutex
01329f79 app.exe    app          24 initialization
774e38f2 KERNEL32.DLL                         BaseThreadInitThunk
What am I missing?
Thanks
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: Mutex

Post by madshi »

ReleaseMutex does *not* free the mutex, it just changes the state of the mutex. You need to use CloseHandle to free the mutex.
emailaya
Posts: 249
Joined: Thu Oct 13, 2005 11:33 am
Contact:

Re: Mutex

Post by emailaya »

Will try and update, thanks
Post Reply