iphone - iAds : How can I prevent my AdBannerView delegate from getting called after the view has been released?

标签 iphone ios ipad iad

我正在将 iAds 添加到我的应用程序,但它崩溃了,因为 AdBannerView 委托(delegate)在 View 释放后被调用。我正在关注 Apple 文档中的代码示例,以及我在 Apple Dev 论坛上找到的代码示例,但我遗漏了一些东西,因为它正在崩溃。

我在 View 的 viewDidLoad 中创建了 AdBannerView,我想在...中显示它

self.bannerIsVisible = NO;
self.adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierLandscape];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
adView.delegate = self;
[self.view addSubview:adView];

我把它放在 viewDidAppear 的前面...

[self.view bringSubviewToFront:[self adView]];
adView.frame = CGRectMake(0.0, 0.0, adView.frame.size.width, adView.frame.size.height);

我在 viewDidUnload 中将它设置为 nil...

[[self adView] setDelegate:nil];
[self setAdView:nil];

然后我在 dealloc 中释放它...

[adView release];

然而,时不时地,应用程序会因此错误而崩溃...

-[MyViewController bannerView:didFailToReceiveAdWithError:]: message sent to deallocated instance 0xf61d820

我错过了什么?

非常感谢您的智慧!

最佳答案

如果 View Controller 最终在卸载 View 之前被释放,则 viewDidUnload 可能永远不会被调用。您还需要在 dealloc 中将委托(delegate)设置为 nil。


此外,我注意到您对广告 View 的内存处理有误。我怀疑您的 adView 属性被声明为 retain 而不是 assign。如果是这样,那么在这一行之后

self.adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];

广告横幅的“保留计数”(不要与 retainCount 属性混淆,后者应被忽略)现在有两个:一个来自分配,一个来自对属性的赋值.如果到达 viewDidUnload,然后 [self setAdView:nil] 释放它一次,但随后引用丢失,因此 dealloc 中的释放永远不会再次释放它以使“保留计数”回到零。

另一方面,如果您的 adView 实际上被声明为 assign 而不是 retain,它仍然是错误的。在这种情况下,viewDidUnload 中的[self setAdView:nil] 会在不释放引用的情况下删除引用,从而泄露广告 View 。

关于iphone - iAds : How can I prevent my AdBannerView delegate from getting called after the view has been released?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5616213/

相关文章:

iphone - 自动滚动 UIScrollView 以适应 native iOS Mail.app 中的内容

iOS 应用程序在 iphone 5 中运行速度很快,但在 iPAD Air 中运行速度很慢

iphone - 使用捏合手势放大 UIImageView

iphone - iOS —将方法映射到数组

iphone - iPhone 上的 Fork()

ios - 默认 MkMapView 区域

ios - iPad:如何模拟 99% 的完整内存?

iPhone - 区分 UIApplicationDidBecomeActiveNotification 通知

ios - UINavigation : Rules on how to present Viewcontroller (push/replace)

ios - 应用程序已批准 - 等待开发人员发布 - 目前在您所在的国家/地区不可用