UIViewController的addSubview,view被释放了

发布于 2024-10-17 08:06:01 字数 649 浏览 4 评论 0原文

编辑: 我自己解决了这个问题。事实证明,这是 dealloc 方法中的剩余部分,导致 UIButton 被释放两次...

我试图在另一个 UIViewController 之上显示一个 UIViewController,就像弹出窗口一样。问题是这个视图似乎被过度发布了。使用 NSZombieEnabled 时,出现以下错误:

[CALayer 释放]:消息发送到已释放的实例 0x784bf40

我使用此代码添加视图:

//self.someViewController is declared as (nonatomic, retain)
self.someViewController = [[[SomeViewController alloc] initWithDelegate:self] autorelease];
[self.view addSubview:self.someViewController.view];

然后,我像这样删除视图:

[self.someViewController.view removeFromSuperview];
self.someViewController = nil;

EDIT:
Solved the problem myself. Turned out it was a leftover in the dealloc method that caused a UIButton to be released twice...

I'm trying to display a UIViewController on top of another UIViewController like a popup. Problem is that the view seems to be getting overreleased. With NSZombieEnabled, I get the following error:

[CALayer release]: message sent to deallocated instance 0x784bf40

I use this code to add the view:

//self.someViewController is declared as (nonatomic, retain)
self.someViewController = [[[SomeViewController alloc] initWithDelegate:self] autorelease];
[self.view addSubview:self.someViewController.view];

Then later on, I remove the view like this:

[self.someViewController.view removeFromSuperview];
self.someViewController = nil;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2024-10-24 08:06:01

如果早期的评论不能解决这个问题,也许这可能会有所帮助。我假设您已经创建了像这样的 someViewController 属性,

@property (nonatomic, retain) NSViewController* someViewController;

在这种情况下,我相信您的代码是正确的(至少我可以看到它应该如何工作),并且您可能会在这里看到二次崩溃。

即,当您调用

self.someViewController = nil;

它时,应该立即释放内存(假设帧已经经过 VC 存在的位置,因此自动释放的计数已经减少)。因此,如果您在 someViewController VC 中使用了另一个对象,该对象仍然存在,并且为您的 someViewController 对象设置了委托,并且正在执行后台任务,那么当它尝试回调到您现在已释放的对象时,将会导致崩溃。 (如果你不释放你的 VC,你不会看到这个崩溃)

例如,如果你有一个 MKMapKit 显示在 someViewController 中,并且委托设置为 someViewController...如果你已经在 someViewController 中实现了该方法,

mapViewDidFinishLoadingMap:(MKMapView*)mapView

MKMapKit 可能仍然如果您之前没有销毁 MKMapView 对象,请从另一个线程调用此方法。

在销毁它用来避免这种风险的所述 VC 之前,我总是将指向您的 VC(如 MKMapView)的其他对象委托设置为 nil。对于 PDF 渲染(CALayer?),您可能会发现您也需要显式释放对象,因为它使用不同的内存分配/释放范例。

Should earlier comments not solve this perhaps this may help. I'm assumning you've created your someViewController property like this

@property (nonatomic, retain) NSViewController* someViewController;

in which case I believe your code is correct (at least I can see how it should work) and you might be seeing a secondary crash here.

I.e. when you're calling

self.someViewController = nil;

this should be freeing the memory up immediately (assuming a frame has gone by where the VC exists so the autoreleased count has already been decreased). Therefore if you have ANOTHER object being used in that someViewController VC who still exists and has a delegate set to your someViewController object and is doing a background task it will cause a crash when it trys to call back to your now deallocated object. (If you don't free your VC you wouldn't see this crash)

For example if you have a MKMapKit being displayed in someViewController and the delegate is set to someViewController... if you have implemented the method in someViewController

mapViewDidFinishLoadingMap:(MKMapView*)mapView

the MKMapKit may still be calling this from another thread if you haven't destroyed MKMapView object before yours.

I would always set other object delegates pointing to your VC (like MKMapView) to nil before destroying the said VC it uses to avoid this risk. For PDF rendering (CALayer?) you may find you need to explicitly release the object too given this uses a different memory alloc/free paradigm.

℡Ms空城旧梦 2024-10-24 08:06:01

我自己解决了这个问题。原来这是 dealloc 方法中的剩余部分导致 UIButton 被释放两次......

Solved the problem myself. Turned out it was a leftover in the dealloc method that caused a UIButton to be released twice...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文