Object is deallocated unexpectedly (EXC_BAD_ACCESS) on iOS


(James Esword) #1

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

(James Esword) #2

This issue seems be missed?


(RemObjects) #3

Thanks, logged as bugs://76838


(Carlo Kok) #6

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


(RemObjects) #7

bugs://76838 got closed with status fixed.


(James Esword) #8

OK, I will retest it.


(James Esword) #9

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


(RemObjects) #10

bugs://76838 got reopened.