Why does short-circuiting not prevent MissingMethodException related to unreachable branch of logical AND (&&)?

北城余情 提交于 2019-12-18 03:05:15

问题


While performing a check if there's a camera present and enabled on my windows mobile unit I encountered something I don't understand.

The code looks like this:

    public static bool CameraP(){

        return Microsoft.WindowsMobile.Status.SystemState.CameraPresent;
    }

    public static bool CameraE()
    {
        return Microsoft.WindowsMobile.Status.SystemState.CameraEnabled;
    }

    public static bool CameraPresent1()
    {
        return Microsoft.WindowsMobile.Status.SystemState.CameraPresent
              && Microsoft.WindowsMobile.Status.SystemState.CameraEnabled;
    }

    public static bool CameraPresent2()
    {
        return CameraP() && CameraE();
    }

When I call CameraPresent2() it return false (there is no camera present). But when I call CameraPresent1() i recieve a MissingMethodException with comment "Could not find method: get_CameraEnabled Microsoft.WindowsMobile.Status.SystemState."

Is the second term evaluated in CameraPresent1 just because they both are property (at language level)?

Is there anything else that explains the difference in behaviour?


回答1:


The second term is not evaluated.

The first term is not evaluated.

The CameraPresent1() method does not even start to execute.

When you call CameraPresent1() for the first time, the runtime must JIT-compile the MSIL into native code. This requires resolving all method calls, even ones that might be reached only conditionally. Compilation fails with the MissingMethodException.

With CameraPresent2(), the call to the getter of CameraEnabled is only compiled when CameraE() is called for the first time, which never happens.




回答2:


C# Specification section 7.12

The && and || operators are called the conditional logical operators. They are also called the “short-circuiting” logical operators.

The && and || operators are conditional versions of the & and | operators:

  • The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is not false.

  • The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is not true.


That is to say that the C# spec guarantees that CameraE() will be called if and only if CameraP() is true.

This may be an issue with aggressive compiler optimizations, and therefore the actual program seems to violate the language spec...


Edit:

Is it possible to put up a breakpoint and show the disassembly window, to see the exact code generated?




回答3:


Just a wild guess, but is it possible that this is a JIT compilation issue? When CameraPresent1 is called, is it attempting to map the call Microsoft.WindowsMobile.Status.SystemState.CameraEnabled to the underlying device? Since it cannot find the method get_CameraEnabled, the entire function fails with a MissingMethodException.




回答4:


Looking at the problem as reported, it seems to make no sense. The two versions should be identical. I wonder, though, if the problem here is that the camera API is using dynamic at some point, and it is trying to look for a true()/false()/& operator. This might convince it to switch to bool logic:

public static bool CameraPresent1()
{
    return ((bool)Microsoft.WindowsMobile.Status.SystemState.CameraPresent)
          && ((bool)Microsoft.WindowsMobile.Status.SystemState.CameraEnabled);
}


来源:https://stackoverflow.com/questions/5233155/why-does-short-circuiting-not-prevent-missingmethodexception-related-to-unreacha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!