ARC 和非 ARC 框架 - iOS - 正确的释放不起作用?
我正在开发一个打开 arc 的 iPad 应用程序。 但我现在使用的是一个没有弧线的小框架。我设置了标志 -fno_objc_arc 等等。 但还剩下一个问题: 框架内的一个对象保存对当前 viewController 的引用。 如果我离开当前的 ViewController,框架会释放控制器属性:
[viewController release];
对象被释放,一切似乎都很好。但仔细观察您会发现,所有实例变量仍然有效且存在。 假设我的 viewController 有两个实例变量 - 一个数组和一个 subviewController。通常在 arc 中,如果主对象被释放,实例变量也会被释放。但是,如果非弧框架发送释放消息,则该对象似乎没有释放其实例变量,从而导致内存错误,因为它们没有在其他任何地方被引用!
编辑:
这里是引用计数,该表是释放的实例变量 viewController:
之前的控制器:1
之前的控制器实例表:1
现在 [viewController 版本] 已发送
之后的控制器:0
之后的控制器实例表: 1
对于这个问题有什么建议吗?我将不胜感激任何帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
亲爱的朋友,使用ARC并不意味着dealloc中没有释放代码。编译器在适当的位置插入释放代码,因此它还会将释放代码插入到您的实例变量中,这是不可见的,因为编译器在编译时插入它,所以当您的非弧框架发送释放消息并且如果您的对象被释放(我假设)它的保留计数为 1,然后调用 dealloc,并且在 dealloc 中也有实例变量的释放代码,该代码也会被释放(如果实例也有 1 个保留计数)
~~~~~~~~~~~~~~~~~~~~已编辑~~~~~~~~~~~~~~~~~~~
使用此代码
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