Click to See Complete Forum and Search --> : DeleteCriticalSection in destructor
mdv
May 25th, 2007, 07:47 AM
Hello,
I was wondering if it is allowed to call DeleteCriticalSection for a critical section declared as a member variable of a class in its destructor. When I do that I seem to get access violation afterwards in some in some unrelated places.
TIA, Denis
wildfrog
May 25th, 2007, 07:56 AM
AFAIK there's nothing wrong with that. Just make sure the critical section object is valid, and not in use, when you destroy it.
- petter
JVene
May 25th, 2007, 08:40 AM
I second wildfrog's point.
To support that, it's important to understand that the critical section object is more like a handle than a C++ object, so calling a function with the word Delete as part of it's name doesn't imply a C++ delete is being called for a C++ object (and the word choice may be unfortunate because it hints that this is so). Instead, the function DeleteCriticalSection is telling the OS to take a destructive action upon the handle (or, perhaps more accurately, the underlying interior structure held by the OS representing the critical section).
TheCPUWizard
May 25th, 2007, 08:47 AM
I third..(well nevermind)...
Actually the most common cause of "unexplained behavior" with regard to CriticalSections that I have come accorss is attempting to use them after they are deleted. For this reason, I wypically wrap them in a class.
1) Constructor Creates or Attachs by name
2) Enter / Leave Methods are public
3) Destructor Detaches or Deletes
mdv
May 25th, 2007, 11:18 AM
Thanks for the replies. Is there a way/tool to figure out what actually happens when an application crashes provided the only information the embedded debugger shows is the access violation address, somewhere beyond the debugging information?
Thanks, Denis.
mdv
May 25th, 2007, 11:20 AM
Thanks for the replies. Is there a way/tool to figure out what actually happens when an application crashes provided the only information the embedded debugger shows is the access violation address, somewhere beyond the debugging information?
Thanks, Denis.
Forgot to mention, looks like this time the crash is not related to critical sections.
kirants
May 25th, 2007, 11:30 AM
You possibly have a memory corruption. Note the critical section is a structure in user space ( and not really a handle ). You might have been seeing the issue because you could possibly have trampled on it and put some garbage in there.
JVene
May 25th, 2007, 08:46 PM
Look into BoundsChecker, HeapAgent and Purify.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.