Article View: borland.public.cppbuilder.nativeapi
Article #1783bizarre behaviour of a mutex handler class and std::auto_ptr
From: static array?
Date: Tue, 19 Jun 2007 15:20
Date: Tue, 19 Jun 2007 15:20
65 lines
2765 bytes
2765 bytes
I require a global mutex for my application to sync resources across
different instances of the app, even running in different user sessions.
I created a class to manage this for me, all I have to do is create an
instance to aquire the mutex and detroy it to release it.
int GlobalMutexReferenceCount = 0;
__fastcall TLicenseMutexHandler::TLicenseMutexHandler(const AnsiString
LicenseProjectName)
{
SetMutexName(LicenseProjectName);
if (++GlobalMutexReferenceCount > 1)
return;
FMutexHandle = CreateMutex(NULL, false, FMutexName.c_str());
WaitForSingleObject(FMutexHandle, INFINITE);
}
__fastcall TLicenseMutexHandler::~TLicenseMutexHandler()
{
if (--GlobalMutexReferenceCount > 0)
return;
ReleaseMutex(FMutexHandle);
CloseHandle(FMutexHandle);
}
void __fastcall TLicenseMutexHandler::SetMutexName(const AnsiString
ALicenseProjectName)
{
FMutexName = "Global\\" + AnsiReplaceText(ALicenseProjectName, "\\", "_");
}
Looks good - OK - so now an example. In the scope of a function that
accesses the protected resource I have:
void __fastcall Func()
{
std::auto_ptr<TLicenseMutexHandler>pLMH(new
TLicenseMutexHandler(MainLFName));
// do stuff to resource
}
** When the function exits, I can see the object gets destroyed correctly
and themutex gets released and closed. However, immediately upon the return
(after the destruction), I get an access violation in the kernel:
First chance exception at 0x7C812A5B. Exception class EAccessViolation with
message 'Access violation at address 00000000...
** Now - here is the BIZARRE part: All I do is change the code to be:
void __fastcall Func()
{
TLicenseMutexHandler * pLMH = new TLicenseMutexHandler(MainLFName);
// do stuff to resource
delete pLMH;
}
And the AV goes away!
Any ideas why?
TIA
Technical Details
Message-ID:
<4677f486@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,1abf1872e1bc6ca3,start X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "static array?" <nospam@nospam.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: bizarre behaviour of a mutex handler class and std::auto_ptr Date: Tue, 19 Jun 2007 08:20:47 -0700 Lines: 67 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138 NNTP-Posting-Host: 207.61.87.125 Message-ID: <4677f486@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1182266502 207.61.87.125 (19 Jun 2007 08:21:42 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail