Object is deallocated unexpectedly (EXC_BAD_ACCESS) on iOS

IDE: VS 2015
Version: 9.0.97.2071
Target (If relevant): iOS
Description:

iOSTest.zip (106.6 KB)

public class DeallocObject {
    public override void dealloc() {
        NSLog("dealloc!!!");    // for breakpoint
    }
}

public class TestClass {

    private static readonly Pool sPool = new Pool(
        (item, arg) => {
            (arg as TestClass).mTag = null;
        });

    internal DeallocObject mTag;

    public void doTest() {
        mTag = sPool.obtain();
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(),
                       () => {
                           sPool.recycle(mTag, this);
                       });
    }
}

public class Pool {

    public delegate void RecycleFunc(DeallocObject item, object arg);
    private readonly RecycleFunc mRecycler;

    public Pool(RecycleFunc recycler) {
        mRecycler = recycler;
    }

    public DeallocObject obtain() {
        return new DeallocObject();
    }

    public void recycle(DeallocObject item, object arg = null) {
        if (item != null) {
            if (mRecycler != null) {
                mRecycler(item, arg);
                /* ATTENTION: The item.dealloc() will be called unexpectedly in mRecycler() */
            }

            /* ERROR: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) */
            var test = item;
        }
    }
}

[UIApplicationMain, IBObject]
class AppDelegate : IUIApplicationDelegate {

    private TestClass mTest = new TestClass();

    public UIWindow window { get; set; }

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

        mTest.doTest();

        return true;
    }

This issue seems be missed?

Thanks, logged as bugs://76838

Can you retest this with latest beta? this should be fixed in fridays build

bugs://76838 got closed with status fixed.

OK, I will retest it.

1 Like

Sorry for the late reply, but this bug is still reproduced in 9.1.100.2103:

bugs://76838 got reopened.