org.sonar.plugins.csharp.S3998.html Maven / Gradle / Ivy
Why is this an issue?
Objects that can be accessed across application
domain boundaries are said to have weak identity. This means that these objects can be considered shared resources outside of the domain, which
can be lead to them being accessed or modified by multiple threads or concurrent parts of a program, outside of the domain.
A thread acquiring a lock on such an object runs the risk of being blocked by another thread in a
different application domain, leading to poor performance and potentially thread starvation and deadlocks.
Types with weak identity are:
- MarshalByRefObject
- ExecutionEngineException
- OutOfMemoryException
- StackOverflowException
- String
- MemberInfo
- ParameterInfo
- Thread
How to fix it
Code examples
Noncompliant code example
public class Sample
{
private readonly StackOverflowException myLock = new();
public void Go()
{
lock (myLock) // Noncompliant
{
// ...
}
}
}
Compliant solution
public class Sample
{
private readonly object myLock = new();
public void Go()
{
lock (myLock)
{
// ...
}
}
}
Resources
Documentation
Articles & blog posts
© 2015 - 2024 Weber Informatics LLC | Privacy Policy