Why FxCop Insists on IDisposable for Structure

In one of my projects, I have code similar to the following:

internal enum ArtworkType { Undefined = 0, Bmp = 1, Gif = 2, Jpeg = 3, Png = 4 } [StructLayout(LayoutKind.Sequential)] internal struct TagArtwork { internal IntPtr data; internal int size; internal ArtworkType type; } 

When I run FxCop in this code, I get warning CA1049 . This structure is used to interact with its own code library, so it has this layout to a large extent. Why does FxCop give me sadness about this structure? I have other structures in the same source file that also have IntPtr members, but FxCop does not complain about them.

For example, the following code does not cause the same warning:

 internal enum ItemType { Implicit = 0, Utf8 = 1, Utf16 = 2, Sjis = 3, Html = 6, Xml = 7, Uuid = 8, Isrc = 9, Mi3p = 10, Gif = 12, Jpeg = 13, Png = 14, Url = 15, Duration = 16, DateTime = 17, Genres = 18, Integer = 21, Riaa_pa = 24, Upc = 25, Bmp = 27, Undefined = 255 } [StructLayout(LayoutKind.Sequential)] internal struct MP4ItmfData { internal byte typeSetIdentifier; internal ItemType typeCode; internal int locale; internal IntPtr value; internal int valueSize; } 

I suppose I could implement IDisposable in a structure, but that just seems to be wrong. Likewise, I could just suppress this warning, but for now I want to understand what this particular structure is that causes a warning when it is not very different from the other seven structures that I have in one source file. Alternatively, I would gladly accept the explanation why other entities do not trigger this warning.

+4
source share
2 answers

The Code Anylsis engine throws this warning whenever you have a managed type that includes a member of what it considers a "native" type. To be a native type, a field must:

  • Be IntPtr , UIntPtr or HandleRef
  • Don't be static
  • Actually assigned a value from native code

I am sure that this third bullet is probably the difference between your different structures. An analysis engine (based on a quick read of dotPeek) will only raise a warning if it really finds an instance of your IntPtr assigned from native code. I have not yet found what he considers "assignment from his own code," but whatever it is, I think that only one of you can use the various types of struct that are part of this rule.

Please note that this is based on reading the actual code of the current implementation of the code-as-ship analysis mechanism from VS2010. Absolutely not documented rule behavior, but probably specific optimization to reduce false positives. You should not assume that the code that currently "passes" this rule (for example, since it was never assigned from its own code) will always do this, since MS is free to change implementation details at any time.

As mentioned in my comment, suppressing a message is a legitimate answer in this case; this is not one of the FxCop rules that should never be suppressed. The rule is very contextual and applies only if you assign your own resource. If you only pass the structure back and forth between C # and the unmanaged code, then you will most likely just suppress the warning and go ahead.

+7
source

This is pretty clearly spelled out in the article you linked:

This rule assumes that the IntPtr, UIntPtr, and HandleRef fields store pointers to unmanaged resources. Types that allocate unmanaged resources must implement IDisposable to allow callers to free these resources on demand and shorten the life of objects that store resources.

Thus, the mere appearance of IntPtr in the structure is enough to trigger a warning. After you have confirmed that you really have not forgotten to free your own resource, apply the [SuppressMessage] attribute in the structure so that it does not view this message again.

+2
source

Source: https://habr.com/ru/post/1444651/


All Articles