Object deallocated before delegate execution on iOS

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

iOSTest2.zip (106.4 KB)

public delegate void Action();

public class TestClass {

    public void test() {
        postDoSth(doSth2);
        postDoSth(doSth1);
    }

    private static void postDoSth(Action action) {
        dispatch_async(dispatch_get_main_queue(), action);
    }

    private void doSth1() {
        NSLog("doSth1!!!"); // ok
    }

    public void doSth2() {
        NSLog("doSth2!!!"); // Problem #1: never be called
    }

    public override void dealloc() {
        NSLog("dealloc!!!");  // Problem #2: called before doSth
    }
}

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

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

Use something like:


        postDoSth(__strong doSth2);
        postDoSth(__strong doSth1);
        // (E28) Unknown type "doSth2"
        postDoSth(__strong doSth2);

seems not working?

sorry:

        postDoSth(__strong(doSth2));
        postDoSth(__strong(doSth1));

By default blocks do a ‘weak’ capture of this/self.

Thank you Carlo!
But I have a more wired problem here:

Oke, that should be fixed now.

1 Like