2023-06-06 16:02:42 +02:00
include::../why-dotnet.adoc[]
2023-05-03 11:06:20 +02:00
2023-06-06 16:02:42 +02:00
== How to fix it
2021-12-02 13:42:27 +00:00
2023-06-06 16:02:42 +02:00
To make sure that a lock is always released correctly, you can follow one of these two methods:
2021-12-02 13:42:27 +00:00
2023-06-06 16:02:42 +02:00
* Use a https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/synclock-statement[`SyncLock`] statement with your lock object.
* Use a https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement[`Try-Finally`] statement and put the release of your lock object within a https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement#finally-block[`Finally`] block.
=== Code examples
==== Noncompliant code example
[source,vbnet,diff-id=1,diff-type=noncompliant]
2023-05-25 14:18:12 +02:00
----
Class Example
Private obj As Object = New Object()
Public Sub DoSomethingWithMonitor()
2023-06-06 16:02:42 +02:00
Monitor.Enter(obj) ' Noncompliant: not all paths release the lock
2023-05-25 14:18:12 +02:00
If IsInitialized() Then
2023-06-06 16:02:42 +02:00
' ...
2023-05-25 14:18:12 +02:00
Monitor.Exit(obj)
End If
End Sub
2023-06-06 16:02:42 +02:00
End Class
----
2023-05-25 14:18:12 +02:00
2023-06-06 16:02:42 +02:00
[source,vbnet,diff-id=2,diff-type=noncompliant]
----
Class Example
2023-05-25 14:18:12 +02:00
Private lockObj As ReaderWriterLockSlim = New ReaderWriterLockSlim()
Public Sub DoSomethingWithReaderWriteLockSlim()
2023-06-06 16:02:42 +02:00
lockObj.EnterReadLock() ' Noncompliant: not all paths release the lock
2023-05-25 14:18:12 +02:00
If IsInitialized() Then
2023-06-06 16:02:42 +02:00
' ...
2023-05-25 14:18:12 +02:00
lockObj.ExitReadLock()
End If
End Sub
End Class
----
2023-06-06 16:02:42 +02:00
==== Compliant solution
2023-05-25 14:18:12 +02:00
2023-06-12 10:24:02 +02:00
[source,vbnet,diff-id=1,diff-type=compliant]
2023-05-25 14:18:12 +02:00
----
Class Example
Private obj As Object = New Object()
Public Sub DoSomethingWithMonitor()
2023-06-07 15:41:34 +02:00
SyncLock obj ' Compliant: the lock will be released at the end of the SyncLock block
2023-06-12 10:24:02 +02:00
If IsInitialized() Then
2023-06-06 16:02:42 +02:00
' ...
2023-05-25 14:18:12 +02:00
End If
End SyncLock
End Sub
2023-06-06 16:02:42 +02:00
End Class
----
2023-05-25 14:18:12 +02:00
2023-06-12 10:24:02 +02:00
[source,vbnet,diff-id=2,diff-type=compliant]
2023-06-06 16:02:42 +02:00
----
Class Example
2023-05-25 14:18:12 +02:00
Private lockObj As ReaderWriterLockSlim = New ReaderWriterLockSlim()
Public Sub DoSomethingWithReaderWriteLockSlim()
2023-06-07 15:41:34 +02:00
lockObj.EnterReadLock() ' Compliant: the lock will be released in the finally block
2023-05-25 14:18:12 +02:00
Try
If IsInitialized() Then
2023-06-06 16:02:42 +02:00
' ...
2023-05-25 14:18:12 +02:00
End If
Finally
lockObj.ExitReadLock()
End Try
End Sub
End Class
----
2023-06-06 16:02:42 +02:00
include::../resources-dotnet.adoc[]
2023-05-25 14:18:12 +02:00
2023-06-06 16:02:42 +02:00
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/synclock-statement[`SyncLock` statement]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement#finally-block[`Finally` block]
2021-12-02 13:42:27 +00:00
2023-06-06 16:02:42 +02:00
include::../rspecator.adoc[]