I am developing an iPad application with arc turned on. But I am now using a little framework with no arc. I set the flag -fno_objc_arc and so on. But there is still one problem left: One object within the framework holds a reference to the current viewController. If I leave the current ViewController, the framework releases the controller property:

[viewController release];

The object gets deallocated and everything seems to be fine. But looking closer u'll recognize, that all instance variables are still valid and existing. Let's assume my viewController has two instance variables to instance variables - an array and a subviewController. Usually in arc, the instance variables are deallocated if the main object gets deallocated. But if the non arc framework sends the release message, it seems that the object dosen't release its instance variables, causing a memory bug because they are not referenced anywhere else!

EDIT:

Here is the reference count, the table is a instance variable of the releasing viewController:

controller before: 1
controller instance table before: 1
now [viewController release] gets sent
controller after: 0
controller instance table after: 1

Any suggestions for this issue? I would appreciate any help!

有帮助吗?

解决方案

Dear friend using ARC doesn't mean that there is no release code in dealloc. The compiler inserts the release code where it is appropriate and thus it also inserts the release code to your instance variables which is not visible because compiler inserts it while compiling so when your non arc framework sends release message and if your object gets deallocated which I assume that it's retain count was 1 then it's dealloc is called and in dealloc there is release code for your instance variables too which also gets deallocated (if instance also have 1 retain count)



~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Edited ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
use this code

NSLog(@"before release : viewController:%d  controllerTable:%d",[viewController retainCount],[controllerTable retainCount]);
[viewController release];
// here just give os to deallocate its memory so I'm just inserting some code which is unnessary but it may give os to reclaim the memory
for (int i=0; i<10; i++) {
    NSLog(@"Just do nothgin here and m also sleeping it to make sure that system get enoght time to reclaim the gabaged memory");
    [NSThread sleepForTimeInterval:0.1];
}
//as it is crashed so commented
//NSLog(@"after release : viewController:%d",[viewController retainCount]);
//it should also crash now
NSLog(@"after release : controllerTable:%d",[controllerTable retainCount]);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top