locked
About analyze in Visual Studio

    Question

  • I analyze my code with Visual Studio, and the result show me:

    CA0001 Error Running Code Analysis CA0001 : Rule=Microsoft.Security#CA2122, Target=LocalWifiLib.UDP.#Stop() : The following error was encountered while reading module 'LocalWiFiClient': Could not resolve member reference: [Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null]Windows.Networking.Sockets.DatagramSocket::Dispose. [Errors and Warnings] (Global)

    what's the problem?


    呵,呵呵~~

    Tuesday, March 27, 2012 8:32 AM

Answers

  • Update: We're looking into this issue.  I've found a few things that are helpful as workarounds until we can get the right fix deployed in a future release.

    The problem here is the explicit call to Dispose().  If you place the variable in a 'using' statement, the compiler inserts a call to Dispose in a hidden try/catch, but it casts the object to IDisposable first.  You can do the same to workaround this problem in code analysis.

    try this instead:

    var sock = new DatagramSocket();
    ((IDisposable)sock).Dispose();

    or:

    using (var sock = new DatagramSocket())
    {
    ...
    }

    Good luck,

    Allen Denver

    Visual Studio Development Lead


    Allen Denver - Visual Studio Development

    • Marked as answer by 流o氓 Thursday, April 5, 2012 2:18 AM
    Wednesday, April 4, 2012 7:23 PM

All replies

  • I analyze my code with VisualStudio, and the result show me:

    CA0001 Error Running Code Analysis CA0001 : Rule=Microsoft.Security#CA2122, Target=LocalWifiLib.UDP.#Stop() : The following error was encountered while reading module 'LocalWiFiClient': Could not resolve member reference: [Windows, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null]Windows.Networking.Sockets.DatagramSocket::Dispose. [Errors and Warnings] (Global)

    what's the problem?


    呵,呵呵~~

    Tuesday, March 27, 2012 8:29 AM
  • It's interesting how the member reference has a scope resolution operator (as in C++) rather than a dot between DatagramSocket and Dispose.  That might be the problem.

    Windows.Networking.Sockets.DatagramSocket  -->::<--   Dispose.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    Tuesday, March 27, 2012 1:58 PM
    Moderator
  • It's interesting how the member reference has a scope resolution operator (as in C++) rather than a dot between DatagramSocket and Dispose.  That might be the problem.

    Windows.Networking.Sockets.DatagramSocket  -->::<--   Dispose.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator

    I use a dot in code, but the Analysis show "::". I donnot know why.

    the Error is about Dispose function. I want the resource which socket hold be cleaned immediately, so call the function Dispose().

    if set null instead of call dispose(), the error will not show, but that will not release the resource immediately, right?

    I don't know which solution is better, or there's another best solution?


    呵,呵呵~~

    Wednesday, March 28, 2012 12:54 AM
  • The error says that DatagramSocket doesn't have a Dispose function, which is technically correct: it has a Close function which gets exposed to C# as Dispose. It sounds like Code Analysis is missing that distinction.

    Since you mention this occurs in Code Analysis is it correct that the code compiles and runs correctly?

    --Rob

    Wednesday, March 28, 2012 5:17 AM
    Owner
  • The error says that DatagramSocket doesn't have a Dispose function, which is technically correct: it has a Close function which gets exposed to C# as Dispose. It sounds like Code Analysis is missing that distinction.

    Since you mention this occurs in Code Analysis is it correct that the code compiles and runs correctly?

    --Rob

    Yes, it compiles and runs correctly.

    So, you means it just a bug of Code Analysis?


    呵,呵呵~~

    Thursday, March 29, 2012 3:12 AM
  • Yes, it compiles and runs correctly.

    So, you means it just a bug of Code Analysis?


    呵,呵呵~~

    Thursday, March 29, 2012 3:14 AM
  • We are tracking this issue and will try to fix it in upcoming release(s).

    Thanks a lot for the feedback, it is really helping us.

    Friday, March 30, 2012 5:43 PM
  • Update: We're looking into this issue.  I've found a few things that are helpful as workarounds until we can get the right fix deployed in a future release.

    The problem here is the explicit call to Dispose().  If you place the variable in a 'using' statement, the compiler inserts a call to Dispose in a hidden try/catch, but it casts the object to IDisposable first.  You can do the same to workaround this problem in code analysis.

    try this instead:

    var sock = new DatagramSocket();
    ((IDisposable)sock).Dispose();

    or:

    using (var sock = new DatagramSocket())
    {
    ...
    }

    Good luck,

    Allen Denver

    Visual Studio Development Lead


    Allen Denver - Visual Studio Development

    • Marked as answer by 流o氓 Thursday, April 5, 2012 2:18 AM
    Wednesday, April 4, 2012 7:23 PM
  • ^_^ `~~

    呵,呵呵~~

    Thursday, April 5, 2012 2:18 AM
  • Thx for your answer.

    呵,呵呵~~

    Thursday, April 5, 2012 2:19 AM
  • FYI: We've identified the problem in the analysis engine and are preparing a fix so that the above workarounds won't be necessary in the future. 

    Once again, thank you very much for this feedback.


    Allen Denver - Visual Studio Development

    Wednesday, April 11, 2012 4:25 PM