Code Inspection: Return value of a property must be disposed by the caller
If you are using the [MustDisposeResourceAttribute] from JetBrains.Annotations to enforce resource disposal in the calling code, you should be aware that the annotation is not inherited if it is placed on a constructor of a base class. This design is motivated by the fact that the derived class may handle the disposal of the resource. In other cases, you need to explicitly annotate the derived class or its constructor with the [MustDisposeResourceAttribute]
to notify its users of the necessity to dispose the resource.
public class HasNativeResources : IDisposable
{
[MustDisposeResource]
public HasNativeResources(object a)
{
// Do something with a
}
[HandlesResourceDisposal] public void Dispose() { }
}
class MayHaveNativeResources : HasNativeResources
{
public MayHaveNativeResources(object b) : base(b)
{
Console.WriteLine();
}
}
public class HasNativeResources : IDisposable
{
[MustDisposeResource]
public HasNativeResources(object a)
{
// Do something with a
}
[HandlesResourceDisposal] public void Dispose() { }
}
class MayHaveNativeResources : HasNativeResources
{
[MustDisposeResource]
public MayHaveNativeResources(object b) : base(b)
{
Console.WriteLine();
}
}
Last modified: 21 March 2024