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