DotNetAnalyzers / IDisposableAnalyzers

Roslyn analyzers for IDisposable
MIT License
384 stars 26 forks source link

How to deal with caches that are responsible for Dispose #552

Open pauldendulk opened 11 months ago

pauldendulk commented 11 months ago

I have a request for advise in a specific situation.

In our library we have a cache with a GetOrCreate method. This is not an uncommon patterns, see, for instance, MemoryCache.GetOrCreateAsync.

The IDisposableAnalyzer warns that the caller should Dispose (Warning IDISP001 Dispose created), but since it is a cache we want to reuse the object later. The cache itself should call Dispose() in a cleanup routine.

This is our method

var paint = vectorCache.GetOrCreatePaint(vectorStyle.Outline, opacity, PolygonRenderer.CreateSkPaint);

CreateSkPaint is a function that is called inside the GetOrCreatePaint method. Turning it into a lambda did not help. Is there another way we could organize our code to circumvent the warning?

pauldendulk commented 6 months ago

I am still interested in a reaction to this question. It looks like the attribute option would help, but that PR is from 2018. What are people using in the mean time? Is there another tool that I should use? I feel like I am missing something. Where is all the fun?