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;
}