bachra04
asked on
CreateMutex and CloseHandle problem
I had a serious problem
In the constructor I created a mutex using :
m_hMutex = ::CreateMutex(NULL, FALSE, NULL);
Then in the destructor
I did:
if (m_hMutex)
{
CloseHandle(m_hMutex);
m_hMutex = NULL;
}
When calling the destructor first time it executes correctly.
When calling it a second time I had crash
Same thing for createThread
Can anyone give an explanation of this problem and how to get rid of that?
In the constructor I created a mutex using :
m_hMutex = ::CreateMutex(NULL, FALSE, NULL);
Then in the destructor
I did:
if (m_hMutex)
{
CloseHandle(m_hMutex);
m_hMutex = NULL;
}
When calling the destructor first time it executes correctly.
When calling it a second time I had crash
Same thing for createThread
Can anyone give an explanation of this problem and how to get rid of that?
A destructor can only be called once. What do you mean when you call it for the second time?
ASKER
Yes I agree,
there was an initial bug that makes the destructor to be called twice.
but the program was running successfully until I added mutex member to my class and I saw the crash.
Now that I fixed the problem I don't have the crash but I'm didn't figured out the problem with mutex
also when using the debugger I saw my m_hmutex=0xfeeefeee.
there was an initial bug that makes the destructor to be called twice.
but the program was running successfully until I added mutex member to my class and I saw the crash.
Now that I fixed the problem I don't have the crash but I'm didn't figured out the problem with mutex
also when using the debugger I saw my m_hmutex=0xfeeefeee.
I still don't understand how you was calling a destructor twice. Except when you are using in-place new you should never be calling a destructor direct. It is called automatically either when the object goes out of scope or when you delete a heap based object. If a destructor gets called more than once the behaviour is undefined. Can you explain how you were managing to call the destructor more than once?
ASKER
as I told you there was some kind of loops
while (...)
{
..
delete MyObject
}
while (...)
{
..
delete MyObject
}
ASKER
Ooups,
There was a bug that makes an extra loop.
I fixed the bug, but my concern from the crash is it may be a hidden problem that was uncovered by this bug.
There was a bug that makes an extra loop.
I fixed the bug, but my concern from the crash is it may be a hidden problem that was uncovered by this bug.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.