Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
My application is crashing with STATUS_RESOURCE_NOT_OWNED (0xC0000264), when LeaveCriticalSection is being called. I am unable to give exact information about how, why etc. There are 15-20 threads running in system, and all use the same critical section.

For example, what can be done with commented line to cause this exception to occur; since I believe, the cause is with modification of CRITICAL_SECTION object.

CRITICAL_SECTION cs;
InitializeCriticalSection(&cs);

EnterCriticalSection(&cs);

// cs.OwningThread =0; - Or Some other direct modification!

LeaveCriticalSection(&cs);


I have searched for internals of CRITICAL_SECTION, but did not find any relevant results.

EDIT: The problem is not with deadlock, improper thread synchronization, initialization of CRITICAL_SECTION. The problem is with the said exception, which can occur if CS is corrupted. And I wanted this exception to be raised by explicitly modifying contents of CRITICAL_SECTION. Do search the net for said exception, and you may find some relevant information.
Posted
Updated 15-Jan-11 5:36am
v4
Comments
Ajay Vijayvargiya 15-Jan-11 11:32am    
Please don't edit title: "NEED" is correct

Are you creating the critical section on the stack? If you are then that's your problem. It's the only thing I can deduct from the provided code :)


This is the wrong way to use critical sections:
void function_running_in_secondary_threads()
{
CRITICAL_SECTION cs;
InitializeCriticalSection(&cs);
EnterCriticalSection(&cs);
  // do work
LeaveCriticalSection(&cs);

}



This isn't glorious coding, I'm just describing how to use a critical section - add error handling/logging/cleanup as needed
CRITICAL_SECTION cs;

void function_executed_before_starting_secondary_threads()
{
 InitializeCriticalSection(&cs);
}

    
void function_running_in_secondary_threads()
 {
 EnterCriticalSection(&cs);
   // do work
 LeaveCriticalSection(&cs);
 }


All calls to EnterCriticalSection and LeaveCriticalSection must reference the same instance of the critical section. Preferably initialize it before starting any secondary threads.

Try using _set_se_translator - copied from documentation
C#
// exceptions_Exception_Handling_Differences3.cpp
// compile with: /EHa
#include <stdio.h>
#include <eh.h>
#include <windows.h>

class SE_Exception {
private:
   SE_Exception() {}
   unsigned int nSE;

public:
   SE_Exception( SE_Exception& e) : nSE(e.nSE) {}
   SE_Exception(unsigned int n) : nSE(n) {}
   ~SE_Exception() {}
   unsigned int getSeNumber() { return nSE; }
};

void SEFunc() {
   __try {
      int x, y = 0;
      x = 5 / y;
    }
    __finally {
      printf_s( "In finally\n" );
   }
}

void trans_func( unsigned int u, _EXCEPTION_POINTERS* pExp ) {
   printf_s( "In trans_func.\n" );
   throw SE_Exception( u );
}

int main() {
   _set_se_translator( trans_func );
    try {
      SEFunc();
    }
    catch( SE_Exception e ) {
      printf_s( "Caught a __try exception with SE_Exception.\n" );
      printf_s( "nSE = 0x%x\n", e.getSeNumber() );
    }
}


Regards
Espen Harlinn
 
Share this answer
 
v4
Comments
Ajay Vijayvargiya 15-Jan-11 11:18am    
I am afraid to say that this doesn't add anything to the problem in hand. All I wanted to raise exception on LeaveCriticalSection. If you see my profile, you can find I know all about multithreading stuff.
Thanks for the answer/comment.

Anyway, I can reproduce the exception:

CRITICAL_SECTION cs;
InitializeCriticalSection(&cs);

EnterCriticalSection(&cs);

// Just random!
cs.LockCount = 111;


LeaveCriticalSection(&cs);
And it causes, the said exception on LeaveCriticalSection!
 
Share this answer
 
v3
With so many threads waiting for the resource to become available, it's quite possible that the request results in a time out.

EnterCriticalSection[^] doesn't wait indefinitely for the resource to become available, but has no return value which would indicate it didn't succeed. You can see if a time out does indeed occur by checking for EXCEPTION_POSSIBLE_DEADLOCK.

Note that MSDN recommends not to handle this exception, it would be prefered to prevent a time out.
 
Share this answer
 
v4
Comments
Ajay Vijayvargiya 15-Jan-11 11:20am    
Timeout with critical-section??

EDIT: I read about timeout registry settings, and yes it is possible to set timeout. I did not know this. Thanks!
But again, this is not relevant to problem in hand. There is critical section in some structure, and that structure is probably being modified, causing the internal state of CRITICAL_SECTION to get modified. And this results in said exception when LeaveCriticalSection is called.
[no name] 15-Jan-11 11:28am    
Yes; the function 'waits for ownership of the specified critical section object'. In this case it has to wait for other threads to be done with it before it's granted access.
The function might not wait that long however, there is a maximum waiting time and if the function times out, it's not visible in a return value but but by checking for EXCEPTION_POSSIBLE_DEADLOCK in a try/except block.
Ajay Vijayvargiya 15-Jan-11 11:31am    
Just edited! :)
BTW, I did not say about "waiting" but about timeout specification while waiting (which I did not know!)
[no name] 15-Jan-11 11:42am    
Maybe I misunderstand what you're after, but I meant the time out indeed to explain the STATUS_RESOURCE_NOT_OWNED exception.
Say you call EnterCriticalSection(&cs), and it times out. This means the critical section object is not yours, but you think it is. When you call LeaveCriticalSection(&cs) on an object you don't own, the program ends with STATUS_RESOURCE_NOT_OWNED exception.

http://msdn.microsoft.com/en-us/magazine/cc163405.aspx
Ajay Vijayvargiya 16-Jan-11 9:33am    
I saw that article, and this exception (as author mentions) may occur when SRW functions.
Ajay,

From what I can see (and as the name of exception suggests), the original problem should be about unbalanced EnterCriticalSection and LeaveCriticalSection.
Your example with random lock count confirms it.

For example, if you leave without entering, you should get the same exception.

C++
CRITICAL_SECTION cs;
InitializeCriticalSection(&cs);

//EnterCriticalSection(&cs);

// Just random!
//cs.LockCount = 111;

LeaveCriticalSection(&cs);


Of course, I can't even think that this possibility could be overlooked by somebody who can really claim "I know all about multithreading stuff", but... would be good to check up. The behavior should be different on different versions of OS (do you use Vista or Windows 7?). Could you reproduce this, too?
 
Share this answer
 
Comments
Ajay Vijayvargiya 16-Jan-11 9:31am    
Let me make it more simple. A CRITICAL_SECTION object is under a struct/class, and that structure is being accessed from somewhere and there is possibility that some buffer overflow may corrupt one of more variables of CRITICAL_SECTION object. A mis-balanced Enter/Leave will not cause this exception.
Sergey Alexandrovich Kryukov 16-Jan-11 18:33pm    
You see, maybe you're looking not for answers but for confirmation of your own version: corrupted critical section object. I cannot imagine such corruption. You did not answer directly: did you try the code above, or just know it would not throw exception? I also want to know your platform. What is it? I will explain why.

Look at this: http://msdn.microsoft.com/en-us/magazine/cc163405.aspx
I was using XP; and I could not reproduce even the effect of "artificial corruption" with "cs.LockCount = 111" you demonstrated. At the same time, I know that threading synchronization primitives are improved form one Windows version to another; example is the light version of upgradeable read/write lock. I would understand of old critical section would be re-implemented using the lock. Come to think about, the STATUS_RESOURCE_NOT_OWNED exception could be a good device for bug detection -- that's why I appreciate your "I need exception".

Please answer my two questions. I hope this will serve you as a hint.

Also, please do me a favour: in case you figure out what's going on, share your experience.

Thank you.
--SA
Ajay Vijayvargiya 17-Jan-11 1:31am    
Windows Server 2008 R2, x64.
Yes, the code is artificial reproduction of the exception. It may not occur on other platforms; as I have seen in WS2003 it doesn't occur.
See, I am not very much concerned about exact reason of exception, but I wanted if there is possibility to have exception when CS object is directly modified. In my original case, An application has crashed, and crash dump analysis as well as application event shows 0xC0000264.
I coded above code to make sure it is not because of unbalanced Enter/Leave, but because of CS corruption - and that's it! :)
Ajay Vijayvargiya 17-Jan-11 1:34am    
"Also, please do me a favour: in case you figure out what's going on, share your experience."

What else do I need to mention? I cannot share the full source code, nor anything about the "product".
Sergey Alexandrovich Kryukov 17-Jan-11 2:07am    
Thank you for information on the platform.

No, I don't mean you code and not even any particular technique you may use. I mean knowledge on the nature of problem. Well, I am confused. Do you have a problem to solve? Is there something you don't understand? I'm trying to help, but I don't know what really happens to your system. Also, I still don't see a convincing evidence of CS corruption. Can you explain all that?

You also never answered my direct question: can you observe (this or different exception) on unbalanced LeaveCriticalSection, as in my sample. Without knowing that I don't know what to say...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900