0

The minimal program below gives a CA2202 warning: Object PList.GetEnumerator() can be disposed more than once in method Module1.Main(). [This is cut-and-pasted from the warning message]/

If you change the List contents to, say, Integer the warning disappears. It also disappears if you take out the surrounding Do loop.

I'm using Visual Studio Community 2017 on Windows 7.

Module Module1

  Sub Main()

    Dim KVP As KeyValuePair(Of Integer, List(Of Object))
    Dim PList As New SortedList(Of Integer, List(Of Object))

    Do While True
      For Each KVP In PList

      Next KVP
    Loop

  End Sub

End Module

I can always ignore the warning but I'm intrigued as to why such a simple program can produce it. Any experts here with any ideas?

Peter
  • 151
  • 6
  • `KVP` is the name assigned to a local variable. Twice. – Jimi Apr 12 '19 at 15:30
  • I disagree. As KVP is defined in its Dim statement, it is used in the For Each statement. Also, removing the DIM statement still gives the error message. – Peter Apr 12 '19 at 17:02
  • VB.Net lets you do that. Try with `For Each KVP As KeyValuePair(Of Integer, List(Of Object)) In PList`. Remove/comment the other declaration of KVP. – Jimi Apr 12 '19 at 17:11
  • Do you mean CA2202 instead of C2202? – TnTinMn Apr 12 '19 at 17:58
  • Thanks @TnTinMn - yes I did! Will edit. – Peter Apr 13 '19 at 08:59
  • Replying to @Jimi - makes no difference! Try it. – Peter Apr 13 '19 at 09:01
  • Posslbly, post code that can reproduce this behaviour and a description of what this code is supposed to accomplish. For example, why do you have a `Do While True` in the Main proc? Is it actually there? To what end? i.e., what is this loop performing? Are you modifying (or trying to) some elements of the collection you're iterating? – Jimi Apr 13 '19 at 09:38
  • @Jimi This is a minimal example to show the spurious warning. It is not a working program - it wouldn't be much use - so it doesn't accomplish anything! The code I posted gives this warning, at least in VS 2017. If you don't believe me try it out yourself. I wouldn't have posted code which doesn't show the warning. Or are you trying to tell me that your IDE doesn't produce the warning? – Peter Apr 13 '19 at 10:01
  • @Peter I don't get the message but I'm running on Win 10 so it may not have anything to do with the code itself. What happens if you change the Sub name to something other than "Main"? – SezMe Apr 13 '19 at 10:46
  • Possible duplicate of [Can a CodeAnalysis return a false positive of CA2202? or is really something wrong with my code?](https://stackoverflow.com/questions/33905634/can-a-codeanalysis-return-a-false-positive-of-ca2202-or-is-really-something-wro) – TnTinMn Apr 13 '19 at 12:17
  • Thanks @TnTinMn there's also a case in https://stackoverflow.com/questions/3831676/ca2202-how-to-solve-this-case?rq=1 which is similar. I'd not found those in my search. Reading the comments there it looks like it's a false positive. But how a code analyser can fail on such a simple case is beyond me. – Peter Apr 13 '19 at 17:25
  • @SezMe - Changing the Sub name makes no difference! I'd tried that but it didn't seem worth while mentioning. I was wrong :( – Peter Apr 13 '19 at 17:27

1 Answers1

0

As @TnTimMn has pointed out, this is a virtual duplicate. The warning seems to be a false positive. Thanks all for your useful comments.

Peter
  • 151
  • 6