[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 954
  • Last Modified:

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?
0
bachra04
Asked:
bachra04
  • 3
  • 3
1 Solution
 
evilrixSenior Software Engineer (Avast)Commented:
A destructor can only be called once. What do you mean when you call it for the second time?
0
 
bachra04Author Commented:
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.
0
 
evilrixSenior Software Engineer (Avast)Commented:
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
bachra04Author Commented:
as I told you there was some kind of loops

while (...)
{
 ..
 delete MyObject

}
0
 
bachra04Author Commented:
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.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> as I told you there was some kind of loops
Ok, I see now. Well, the act of calling delete on a pointer that has already had delete called on it is undefined... so that's pretty much your answer.

>> I fixed the bug, but my concern from the crash is it may be a hidden problem that was uncovered by this bug.
In this case the undefined behaviour manifested as a crash, now you've fix the issue the crash has stopped. This is what I'd expect.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now