All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.S3998.html Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version

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:

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