Monitor.Exit Exception

Any reason why i get an exception when Monitor.Exit() executes

     try
       {

Monitor.Enter(someSharedResource);

//DOING STUFF
catch (Exception ex)
{
return ex.ToString();
}
finally
{
Monitor.Exit(someSharedResource); //get exception?
}

It would help to know exactly what the exception is and the stack trace. Please catch it in the debugger and provide complete information so that we can properly assist.

Exception System.Exception - CLR_E_LOCK_SYNCHRONIZATION_EXCEPTION (1)

#### Message: 
#### System.Threading.Monitor::Exit [IP: 0000] ####
#### ZigmateGadgeteer.API::SensorController [IP: 0370] ####
#### ZigmateGadgeteer.API::ProcessApi [IP: 0558] ####
#### ZigmateGadgeteer.Program::RelayDevice [IP: 0063] ####
#### ZigmateGadgeteer.Program::ProgramStarted [IP: 0493] ####
#### ZigmateGadgeteer.Program::Main [IP: 0015] ####

A first chance exception of type ‚ÄėSystem.Exception‚Äô occurred in mscorlib.dll

In looking at the CLR source code, this exception is only triggered one place. It looks like it will be triggered if you exit a monitor that was never entered, exit a monitor more times than it was entered, or exit on a thread other than the owning thread.

The relevant CLR code is copied below (this is the only place that exception is thrown):

HRESULT CLR_RT_HeapBlock_Lock::DecrementOwnership( CLR_RT_HeapBlock_Lock* lock, CLR_RT_SubThread* sth )
{
    NATIVE_PROFILE_CLR_CORE();
    TINYCLR_HEADER();

    CLR_RT_Thread* th = sth->m_owningThread;

    if(lock && lock->m_owningThread == th)
    {
        TINYCLR_FOREACH_NODE(CLR_RT_HeapBlock_Lock::Owner,owner,lock->m_owners)
        {
            if(owner->m_owningSubThread == sth)
            {
                if(--owner->m_recursion == 0)
                {
                    g_CLR_RT_EventCache.Append_Node( owner );
                }

                //--//

                if(lock->m_owners.IsEmpty())
                {
                    lock->ChangeOwner();
                }

                TINYCLR_SET_AND_LEAVE(S_OK);
            }
        }
        TINYCLR_FOREACH_NODE_END();
    }

    TINYCLR_SET_AND_LEAVE(CLR_E_LOCK_SYNCHRONIZATION_EXCEPTION);

    TINYCLR_NOCLEANUP();
}

3 Likes

here is my output…it appears like i have a corresponding Monitor.enter and Monitor.exit but still get exception

Monitor.Enter…82
Monitor.Exit…82
#### Exception System.Exception - CLR_E_LOCK_SYNCHRONIZATION_EXCEPTION (3) ####
#### Message:
#### System.Threading.Monitor::Exit [IP: 0000] ####
#### ZigmateGadgeteer.API::SensorController [IP: 0391] ####
#### ZigmateGadgeteer.API::ProcessApi [IP: 0558] ####
#### ZigmateGadgeteer.Program::OnPlayBackEvent [IP: 003c] ####
#### ZigmateGadgeteer.Program::IntButton_OnInterrupt [IP: 0017] ####
A first chance exception of type ‚ÄėSystem.Exception‚Äô occurred in mscorlib.dll
11/11/2015 08:17:41|4|API::ProcessApi|2|IntButton_OnInterrupt|sensors/LED02/Off
11/11/2015 08:17:41|16||Controller::sensors
11/11/2015 08:17:41|16||id::led02
11/11/2015 08:17:41|16||action::off
Monitor.Enter…83
Monitor.Exit…83
#### Exception System.Exception - CLR_E_LOCK_SYNCHRONIZATION_EXCEPTION (3) ####
#### Message:
#### System.Threading.Monitor::Exit [IP: 0000] ####
#### ZigmateGadgeteer.API::SensorController [IP: 0391] ####
#### ZigmateGadgeteer.API::ProcessApi [IP: 0558] ####
#### ZigmateGadgeteer.Program::OnPlayBackEvent [IP: 0055] ####
#### ZigmateGadgeteer.Program::IntButton_OnInterrupt [IP: 0017] ####
A first chance exception of type ‚ÄėSystem.Exception‚Äô occurred in mscorlib.dll
…OnPlayBackEvent[148523]
…IntButton_OnInterrupt[148525]
IntButton_OnInterrupt[166213]…
OnPlayBackEvent[166214]…
11/11/2015 08:17:59|4|API::ProcessApi|2|IntButton_OnInterrupt|sensors/LED02/Off
11/11/2015 08:17:59|16||Controller::sensors
11/11/2015 08:17:59|16||id::led02
11/11/2015 08:17:59|16||action::off
Monitor.Enter…84
Monitor.Exit…84
#### Exception System.Exception - CLR_E_LOCK_SYNCHRONIZATION_EXCEPTION (3) ####
#### Message:
#### System.Threading.Monitor::Exit [IP: 0000] ####
#### ZigmateGadgeteer.API::SensorController [IP: 0391] ####
#### ZigmateGadgeteer.API::ProcessApi [IP: 0558] ####
#### ZigmateGadgeteer.Program::OnPlayBackEvent [IP: 0023] ####
#### ZigmateGadgeteer.Program::IntButton_OnInterrupt [IP: 0017] ####
A first chance exception of type ‚ÄėSystem.Exception‚Äô occurred in mscorlib.dll
11/11/2015 08:17:59|4|API::ProcessApi|2|IntButton_OnInterrupt|sensors/LED02/On
11/11/2015 08:17:59|16||Controller::sensors
11/11/2015 08:17:59|16||id::led02
11/11/2015 08:17:59|16||action::on
Monitor.Enter…85
Monitor.Exit…85
#### Exception System.Exception - CLR_E_LOCK_SYNCHRONIZATION_EXCEPTION (3) ####
#### Message:
#### System.Threading.Monitor::Exit [IP: 0000] ####
#### ZigmateGadgeteer.API::SensorController [IP: 0391] ####
#### ZigmateGadgeteer.API::ProcessApi [IP: 0558] ####
#### ZigmateGadgeteer.Program::OnPlayBackEvent [IP: 003c] ####
#### ZigmateGadgeteer.Program::IntButton_OnInterrupt [IP: 0017] ####
A first chance exception of type ‚ÄėSystem.Exception‚Äô occurred in mscorlib.dll
11/11/2015 08:18:00|4|API::ProcessApi|2|IntButton_OnInterrupt|sensors/LED02/Off
11/11/2015 08:18:00|16||Controller::sensors
11/11/2015 08:18:00|16||id::led02
11/11/2015 08:18:00|16||action::off
Monitor.Enter…86
Monitor.Exit…86
#### Exception System.Exception - CLR_E_LOCK_SYNCHRONIZATION_EXCEPTION (3) ####
#### Message:
#### System.Threading.Monitor::Exit [IP: 0000] ####
#### ZigmateGadgeteer.API::SensorController [IP: 0391] ####
#### ZigmateGadgeteer.API::ProcessApi [IP: 0558] ####
#### ZigmateGadgeteer.Program::OnPlayBackEvent [IP: 0055] ####
#### ZigmateGadgeteer.Program::IntButton_OnInterrupt [IP: 0017] ####
A first chance exception of type ‚ÄėSystem.Exception‚Äô occurred in mscorlib.dll
…OnPlayBackEvent[167319]
…IntButton_OnInterrupt[167320]
11/11/2015 08:18:05|4||Start::timer_TicktimerSDCard
11/11/2015 08:18:06|4||LSB: 249
11/11/2015 08:18:06|4||MSB: 249
11/11/2015 08:18:06|4||Temperature: 15
11/11/2015 08:18:06|4|IO|11/11/2015 08:18:06|0|1|15|0

11/11/2015 08:18:06|4||UDP:LogIT|55|11/11/2015 08:18:06|4|IO|11/11/2015 08:18:06|0|1|15|0
|
IntButton_OnInterrupt[177583]…
OnPlayBackEvent[177585]…
11/11/2015 08:18:10|4|API::ProcessApi|2|IntButton_OnInterrupt|sensors/LED02/Off
11/11/2015 08:18:10|16||Controller::sensors
11/11/2015 08:18:10|16||id::led02
11/11/2015 08:18:10|16||action::off
Monitor.Enter…87
Monitor.Exit…87
#### Exception System.Exception - CLR_E_LOCK_SYNCHRONIZATION_EXCEPTION (3) ####
#### Message:
#### System.Threading.Monitor::Exit [IP: 0000] ####
#### ZigmateGadgeteer.API::SensorController [IP: 0391] ####
#### ZigmateGadgeteer.API::ProcessApi [IP: 0558] ####
#### ZigmateGadgeteer.Program::OnPlayBackEvent [IP: 0023] ####
#### ZigmateGadgeteer.Program::IntButton_OnInterrupt [IP: 0017] ####
A first chance exception of type ‚ÄėSystem.Exception‚Äô occurred in mscorlib.dll
The program ‚Äė[31] Micro Framework application: Managed‚Äô has exited with code 0 (0x0).

Monitor.Enter(someSharedResource );

Do i have to do anything with the variable someSharedResource? Its set as 1, and i never change it.

Even when i put this code in Main program i get the error?

            try
            {

                Monitor.Enter(someSharedResource);

                Debug.Print("Monitor.Enter.." + someSharedResource);
              
            }
            catch (Exception ex)
            {
                //  return ex.ToString();
            }
            finally
            {
                Debug.Print("Monitor.Exit.." + someSharedResource);
                Monitor.Exit(someSharedResource);

            }

Try with an object type for ‚ÄúsomeSharedResource‚ÄĚ.

@ Bec a Fuel - Fixed it…thank you

What does someSharedResource do anyway? Do i have to create a new variable for every resource i am protecting?

@ Bec A Fuel is correct. Under the covers, the code locks a block of heap memory, so you need a reference object (not an int, float, double,…) but something that inherits from object. Use the code @ Bec A Fuel provided for the lockable resource.

If you continue to have problems, please post a complete code example, pared down to the minimum code it takes to reproduce the problem. WIthout that, we’re just guessing.