Click to See Complete Forum and Search --> : Access violation calling DeleteCriticalSection()


philkr
February 25th, 2006, 01:07 PM
The title describes it enough, I think. I don't know much about critical sections and copied the code from someone else. But I have run the debugger and got this additional information:

LockCount = -1
RecursionCount = 0
OwningThread = 0
LockSemaphore = 0
SpinCount = 0

I really don't know waht all these values mean, but OwningThread = 0 seems suspicious to me. What do you say?

Siddhartha
February 25th, 2006, 06:17 PM
I really don't know waht all these values mean...These are member values of the structure CRITICAL_SECTION (and ideally, you don't need to be worrying about them up for DeleteCriticalSection to work fine... In fact, you dont need to be manipulating any value in there to work with Critical Section APIs.)

Perhaps, you should look this up -

Post: Using Critical Sections (http://www.codeguru.com/forum/showthread.php?p=1252692#post1252692)

philkr
February 26th, 2006, 06:56 AM
Can it be that this happens, when the worker threads have not finished when I call DeleteCriticalSection?

Siddhartha
February 26th, 2006, 06:57 AM
Well... One is not supposed to delete a critical section object and then use that same object after.

This would be an invitation to trouble aplenty... :)

Siddhartha
February 26th, 2006, 07:02 AM
...For that matter, from an implementation point of view - A CS object must not be deleted more than once.

Ditto for initialization.

philkr
February 26th, 2006, 07:15 AM
...For that matter, from an implementation point of view - A CS object must not be deleted more than once.

Ditto for initialization.
Yes that was exactly the error. I made a for-loop with an if-statement right before and the compiler "thought" DeleteCriticalSection() was within the for loop. And I thought only the first statement after for counts to the loop unless you make brackets. This is what I had done:

for(...)
if(...)
DoSomething();
DeleteCriticalSection();

So why does the compiler count DeleteCriticalSection() to the loop? I fixed it with brackets, but I want to know the reason before I make the mistake again.

Siddhartha
February 26th, 2006, 08:20 AM
So why does the compiler count DeleteCriticalSection() to the loop? I fixed it with brackets...Are you sure your compiler did that... ?

Verify behaviour using a test sample...

/ I am looking up the standards document. When I find something interesting, I'll post it here.

Siddhartha
February 26th, 2006, 01:43 PM
I changed the post above... FYI.

philkr
February 27th, 2006, 05:18 AM
Are you sure your compiler did that... ?

Verify behaviour using a test sample...

/ I am looking up the standards document. When I find something interesting, I'll post it here.
That is exactly what my compiler does. I verified it by single-stepping through that specific part. BTW, I am using MS Visual Studio 2005 Professional.

Siddhartha
February 27th, 2006, 05:25 AM
That is exactly what my compiler does. I verified it by single-stepping through that specific part. BTW, I am using MS Visual Studio 2005 Professional.Actually, that is the misleading part perhaps... The Visual Studio IDE will take you to the line after the if (...) construct, but does not execute it.

Say, in this code -
for (int nTest = 0; nTest < 50; ++nTest)
if (nTest < 50)
std::cout << nTest << std::endl;

WriteSomethingToScreen (); ...The VS IDE will take the execution pointer to WriteSomethingToScreen, but really not execute it (this you realize only when WriteSomethingToScreen writes a visible output) before it jumps back to the for (...) loop.

philkr
February 27th, 2006, 05:45 AM
Let me describe a bit more clearly what I did using your example:
The instruction pointer was at this line:
WriteSomethingToScreen()
Then I pressed F10 to single-step. After that the execution pointer was on this line:
for(int nTest = 0; nTest < 50; ++nTest)

Siddhartha
February 27th, 2006, 05:52 AM
Let me describe a bit more clearly what I did using your example:
The instruction pointer was at this line:
WriteSomethingToScreen()
Then I pressed F10 to single-step. After that the execution pointer was on this line:
for(int nTest = 0; nTest < 50; ++nTest)Precisely... And even though the instruction pointer was on WriteSomethingToScreen (...), it would not execute it.

I hope I am clear...

philkr
February 27th, 2006, 06:19 AM
I understand. But now I am totally confused, because using the brackets fixed my problem.

Siddhartha
February 27th, 2006, 06:21 AM
...Post your code... I mean the lines in error, and the lines around it.

philkr
February 27th, 2006, 06:41 AM
CWaveOut::~CWaveOut()
{
waveOutReset(m_hWaveOut);
for(int i = 0; i < m_nFreeBufferCount; ++i)
if(m_pWaveHeaders[i].dwFlags & WHDR_PREPARED)
waveOutUnprepareHeader(m_hWaveOut, &m_pWaveHeaders[i], sizeof(WAVEHDR));
DeleteCriticalSection(&waveCriticalSection);
HeapFree(GetProcessHeap(), 0, m_pWaveHeaders);
waveOutClose(m_hWaveOut);
ExitMP3(&m_dMp3Decoder);
}