Редактиране

Споделяне чрез


CA2002: Do not lock on objects with weak identity

Property Value
Rule ID CA2002
Title Do not lock on objects with weak identity
Category Reliability
Fix is breaking or non-breaking Non-breaking
Enabled by default in .NET 8 No

Cause

A thread attempts to acquire a lock on an object that has a weak identity.

Rule description

An object is said to have a weak identity when it can be directly accessed across application domain boundaries. A thread that tries to acquire a lock on an object that has a weak identity can be blocked by a second thread in a different application domain that has a lock on the same object.

The following types have a weak identity and are flagged by the rule:

How to fix violations

To fix a violation of this rule, use an object from a type that is not in the list in the Description section.

When to suppress warnings

It is safe to suppress the warning if the locked object is this or Me and the visibility of the self object type is private or internal, and the instance is not accessible using any public reference.

Otherwise, do not suppress a warning from this rule.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable CA2002
// The code that's violating the rule is on this line.
#pragma warning restore CA2002

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.CA2002.severity = none

For more information, see How to suppress code analysis warnings.

CA2213: Disposable fields should be disposed

Example

The following example shows some object locks that violate the rule.

Imports System
Imports System.IO
Imports System.Reflection
Imports System.Threading

Namespace ca2002

    Class WeakIdentities

        Sub SyncLockOnWeakId1()

            SyncLock GetType(WeakIdentities)
            End SyncLock

        End Sub

        Sub SyncLockOnWeakId2()

            Dim stream As New MemoryStream()
            SyncLock stream
            End SyncLock

        End Sub

        Sub SyncLockOnWeakId3()

            SyncLock "string"
            End SyncLock

        End Sub

        Sub SyncLockOnWeakId4()

            Dim member As MemberInfo =
            Me.GetType().GetMember("SyncLockOnWeakId1")(0)
            SyncLock member
            End SyncLock

        End Sub

        Sub SyncLockOnWeakId5()

            Dim outOfMemory As New OutOfMemoryException()
            SyncLock outOfMemory
            End SyncLock

        End Sub

    End Class

End Namespace
class WeakIdentities
{
    void LockOnWeakId1()
    {
        lock (typeof(WeakIdentities)) { }
    }

    void LockOnWeakId2()
    {
        MemoryStream stream = new MemoryStream();
        lock (stream) { }
    }

    void LockOnWeakId3()
    {
        lock ("string") { }
    }

    void LockOnWeakId4()
    {
        MemberInfo member = this.GetType().GetMember("LockOnWeakId1")[0];
        lock (member) { }
    }
    void LockOnWeakId5()
    {
        OutOfMemoryException outOfMemory = new OutOfMemoryException();
        lock (outOfMemory) { }
    }
}

See also