Delegate is executed in wrong context on iOS

IDE: VS 2015
Version: 8.4.96.2023
Target (If relevant): iOS + C#
Description:

Found this issue when I try to add a cached delegate member field to my existed class. Finally I can construct a small test case to repro it:

iOSTest2.zip (106.5 KB)

public delegate void Action();

public class TestClass {

    private Action mFunc;
    private int mTagValue;

    public void test() {

        // If this line is removed, then everything's ok. Really wired.
        if (mFunc == null) mFunc = doSth;

        mTagValue = 999;
        dispatch_async(dispatch_get_main_queue(), doSth);
    }

    private void doSth() {
        // This is expected to print "doSth: 999"
        // But it will print a random number, seems like this member function is running in a wrong context. 
        // Maybe the captured this pointer is wrong?
        NSLog("doSth: " + mTagValue);
    }
}

[UIApplicationMain, IBObject]
class AppDelegate : IUIApplicationDelegate
{
    public UIWindow window { get; set; }

    // Ensure the obj won't be deallocated
    private TestClass mRef;

    public BOOL application(UIApplication application) didFinishLaunchingWithOptions(NSDictionary launchOptions)
    {
        window = new UIWindow(UIScreen.mainScreen().bounds);
        window.rootViewController = new UIViewController withNibName(null) bundle(null);
        window.makeKeyAndVisible();
        mRef = new TestClass();
        mRef.test();
        return true;
    }

What does “context” mean in this, well, context?

I use “context” because not sure how to describe the problem exactly…
It seems the member function is executed with a wrong this pointer. I think, in a sense, the “this pointer” is the context of a member function.

Seems to be something “weak” related. using __strong it does work. I’ll investigate more.

Thanks, logged as bugs://75760

bugs://75760 got closed with status fixed.

Logged as bugs://i63716.

bugs://i63716 was closed as fixed.