Delegate will cause objc_weak_error when dealloc on iOS


(James Esword) #1

IDE: VS 2015
Version: 8.3.94.2001 (I can’t debug iOS app on newer beta version)
Target (If relevant): iOS + C#
Description:

I observed this issue when I was doing unit tests, in which I use C# delegates a lot. The output window prints many errors and the error looks like:

objc[1711]: __weak variable at 0x7fff574331b8 holds 0x7fc102caee48 instead of 0x7fc102cac8c0. This is probably incorrect use of objc_storeWeak() and objc_loadWeak(). Break on objc_weak_error to debug.
(the source code: https://opensource.apple.com/source/objc4/objc4-646/runtime/objc-weak.mm)

Finally I’m able to build a simple demo to reproduce it. It seems like the implementation of “delegate” on iOS has something wrong.

IOSApp1.zip (107.0 KB)

public class TestClass {

    public delegate int Comparer(object a, object b);

    public TestClass() {
        sort(test); // this will cause that objc_weak_error
    }

    public int test(object a, object b) {
        return 0;
    }

    public void sort(Comparer comparer) {
    }

    public override void dealloc() {
        NSLog("dealloc TestClass!");
        // After this dealloc, the output window will always print an error info:
        // objc[1711]: __weak variable at 0x7fff574331b8 holds 0x7fc102caee48 instead of 0x7fc102cac8c0. This is probably incorrect use of objc_storeWeak() and objc_loadWeak(). Break on objc_weak_error to debug.
    }
}

[UIApplicationMain, IBObject]
class AppDelegate : IUIApplicationDelegate {

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

        // create & destroy immediately
        new TestClass();

        return true;
    }
    ...


(RemObjects) #2

Thanks, logged as bugs://75554


(RemObjects) #3

bugs://75554 got closed with status fixed.


(James Esword) #4

@ck This bugfix is included in the change log of 8.4.96.2017, but I can still reproduce it in 8.4.96.2019.


(RemObjects) #5

bugs://75554 got reopened.


(Carlo Kok) #6

Strange. Reopened.


(RemObjects) #7

bugs://75554 got closed with status fixed.