AdMob插页式内存泄漏?精灵套件

时间:2016-03-19 18:26:29

标签: objective-c memory-leaks sprite-kit admob

我遇到了AdMob和sprite kit的问题。

每次广告展示或加载时,都会大量增加内存使用量! 当EndGameScene打开时,它将调用“showAd”,并且当按下重播按钮时“loadAd”。

广告效果很好。

当游戏启动时,内存使用量大约为50mb,之后的广告数量至少为70-100mb!

所以我做错了:

GameViewController.m

- (void)viewDidLoad{
[super viewDidLoad];



[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"showAd" object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"loadAd" object:nil];

self.interstitial = [self createAndLoadInterstitial];
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-<id>"];

GADRequest *request = [GADRequest request];
// Requests test ads on test devices.
request.testDevices = @[@"0000000"];
[self.interstitial loadRequest:request];

//etc
}


- (void)handleNotification:(NSNotification *)notification{
if ([notification.name isEqualToString:@"showAd"]) {
    if (self.interstitial.isReady) {
        [ self.interstitial presentFromRootViewController:self];
    }

    else {

    }

}

if ([notification.name isEqualToString:@"loadAd"]) {
   self.interstitial = [self createAndLoadInterstitial];
}
}
- (GADInterstitial *)createAndLoadInterstitial {
GADInterstitial *interstitial =
[[GADInterstitial alloc] initWithAdUnitID:@"ca-app-pub-<id>"];
interstitial.delegate = self;
[interstitial loadRequest:[GADRequest request]];
return interstitial;}

谢谢!

1 个答案:

答案 0 :(得分:1)

每次创建新的GADInterstitial对象时都会创建,该对象会启动加载请求。我猜是因为它正在等待答案,它永远不会再从内存中释放出来。 如果您需要新广告,更好的方法是将GADInterstitial存储在全局属性中,并调用新的loadRequest。