🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #15275

Mutex and WaitForSingleObject Problem...

#15275
From: Colin Maharaj
Date: Tue, 15 Jul 2003 01:23
49 lines
2543 bytes
When I run the memory profiler I get the error 'Trying to free
unexisting resource'. If I remove the 
Mutex and WaitForSingleObject Code, I do not get the error but run the
risk of having my 
consumer application try to access the 'OutFile' while this process is
writing to it.
Note: I have the same type of synchronization code in the consumer
application as well.

I can probably solve this by having my consumer app try to have
non-shared access 
to the file - in a loop - until it can - but this not an elegant
solution.

Thanks for any assistance.

My Code Snippet......


         /// ensure it is being accessed by one process at once
          HANDLE BinAccess = CreateMutex ( NULL, FALSE, "BinAccess");
          bool DidCreate = true;
          if (BinAccess == NULL)
          {
            if (GetLastError() == ERROR_ALREADY_EXISTS)
            {
              BinAccess = OpenMutex ( NULL, FALSE, "BinAccess");
              DidCreate = false;
            }
            else
            {
              Beep(2000,10); // some weird error.....
            }
          }

          WaitForSingleObject(BinAccess, INFINITE);

          int iFileHandle;
          if (FileExists(OutFile))  iFileHandle = FileOpen(OutFile,
fmOpenWrite);
          else                      iFileHandle = FileCreate(OutFile);

          // File Output Processing foo bars here........

          FileClose(iFileHandle);

          ReleaseMutex(BinAccess);
          if (DidCreate == true) CloseHandle(BinAccess);
Technical Details
Message-ID: <3F135784.B6BE9C00@cablenett.net>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-05!sn-xit-08!supernews.com!small1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,4b66d79a94ed9589,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-07-14 18:39:39 PST
Message-ID: <3F135784.B6BE9C00@cablenett.net>
Date: Mon, 14 Jul 2003 21:23:16 -0400
From: Colin Maharaj <cbm@cablenett.net>
Reply-To: cbm@cablenett.net
X-Mailer: Mozilla 4.78 [en] (Windows NT 5.0; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Mutex and WaitForSingleObject Problem...
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 196.32.37.92
X-Trace: newsgroups.borland.com 1058232437 196.32.37.92 (14 Jul 2003 18:27:17 -0700)
Lines: 49
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-05!sn-xit-08!supernews.com!small1.nntp.aus1.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail