为了保证可执行程序只运行一份,我在工程创建的时候加了如下语句:
CreateMutex(nil,   False,  'HST_RSGZ');
  if GetLastError= ERROR_ALREADY_EXISTS then
  begin
    messagebox(application.handle,'程序已经打开在系统状态栏中!','提示',mb_ok);
    exit;
  end;内存检测工具说这儿还有内存没有释放,
请问这里创建的mutex该如何释放,在何时释放?

解决方案 »

  1.   

    var
      hMutex: HWND;

    hMutex := CreateMutex(nil,  False,  'HST_RSGZ'); 

    ReleaseMutex(hMutex); //释放
      

  2.   

    mutex是内核对象,显然是要用CloseHandle才能释放
    ReleaseMutex最多只能让mutex处于abandoned状态
      

  3.   

    . Relase  後需 再執行下 CloseHandle
      

  4.   

    Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed. 當然在進程關閉時也會自動關閉