OpenFlow 在尝试重置/重新创建时导致应用程序崩溃
我正在拼命尝试更改 OpenFlow 实例上的图像,但没有成功。 我有点放弃了,所以我现在尝试删除该实例并创建一个新实例。但我无法避免应用程序崩溃。
创建它的代码是:
AFOpenFlowView *of = [[AFOpenFlowView alloc] initWithFrame:CGRectMake(0, 100, 320, 380)];
[of setCenter:CGPointMake(160, 240)];
[of setBackgroundColor:[UIColor blackColor]];
[of setDataSource:self];
[of setViewDelegate:self];
[self setPeopleFlow:of];
[self.view addSubview:peopleFlow];
[of release];
然后,单击一个按钮,我执行以下操作:
[peopleFlow removeFromSuperview];
[peopleFlow release];
稍后,我使用第一段代码调用相同的函数来再次创建它,此时应用程序崩溃且没有日志错误。
关于如何清理 OpenFlow 对象以重新填充它而无需删除/重新创建的任何想法?或者如何可靠地创建/重新创建?
I am trying desperately to change the images on an OpenFlow instance with no luck.
I am kind of giving up so I am trying now to remove the instance and create a new one. But I can't avoid crashing the app.
The code to create it is:
AFOpenFlowView *of = [[AFOpenFlowView alloc] initWithFrame:CGRectMake(0, 100, 320, 380)];
[of setCenter:CGPointMake(160, 240)];
[of setBackgroundColor:[UIColor blackColor]];
[of setDataSource:self];
[of setViewDelegate:self];
[self setPeopleFlow:of];
[self.view addSubview:peopleFlow];
[of release];
Then, on a click of a button I do:
[peopleFlow removeFromSuperview];
[peopleFlow release];
Later on I call the same function with the first block of code to create it again and it's when the application crashes with no log error.
Any ideas on how to clean the OpenFlow object to repopulate it without having to remove/recreate? Or how create/recreate reliably?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建peopleFlow实例时,它的保留计数为1。
然后当您将其添加为子视图时,超级视图保留它,因此它的保留计数为2。
然后将其添加到超级视图后释放它,所以它是保留计数再次为 1。
然后你从超级视图中删除它,超级视图释放它,所以它的保留计数为0并且对象被释放。
然后您尝试再次释放它,但它崩溃了,因为您正在向已释放的对象发送
release
。长话短说,在这种情况下,从超级视图中删除它后不需要释放它。
另外,如果你释放了一个指针并且不再关心它所指向的内容,那么将 nil 分配给它也是一个很好的做法。这是因为在释放和释放一个对象后,指针变量仍然指向该对象曾经占用的内存。如果您尝试向悬空指针所指向的任何内容发送消息,则将 nil 分配给指针可以防止发生任何不良情况。
When you create the peopleFlow instance, it has a retain count of 1.
Then when you add it as a subview, the super view retains it, so it's retain count is 2.
Then you release it after adding it to the superview, so it's retain count is 1 again.
Then You remove it from the superview, and the superview releases it, so it's retain count is 0 and the object gets deallocated.
Then you try and release it again, and it crashes because you're sending
release
to a deallocated object.Long story short, in this case, you don't need to release it after removing it from the superview.
Also, it is good practise to assign nil to a pointer if you release it and don't care about what is pointing at any more. This is because after you release and deallocate an object, the pointer variable is still pointing to the memory that the object used to occupy. Assigning nil to the pointer prevents any bad stuff happening if you try to send a message to whatever the dangling pointer is pointing to.