简体   繁体   中英

ios iad and admob intergration causing memory leak?

I use this code where if an iad fails to load it looks for an admob. Everything appears to work fine except in instruments I have noticed a large memory spike anytime an admob gets called. After putting this through instruments multiple times I only got a memory leak once which im pretty sure occurred when an admob was called. I have seen some people talk about memory leaks with admob but i wasn't sure if this was fixed or not.

Does my code look good? If so hopefully this helps someone out but I may end up taking admob out of my app cause it seems to drastically slow down the program after awhile. Also I did not realize the sdk is close to 8mb.

-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!self.bannerIsVisible) {
        [bannerView_ removeFromSuperview];
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0.0, -50.0);
        [UIView commitAnimations];
        self.bannerIsVisible = YES;

    }
}

-(void)callAdMob {
    // Create a view of the standard size at the bottom of the screen.
    bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0,
                                            self.view.frame.size.height -
                                            GAD_SIZE_320x50.height,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];

    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    bannerView_.adUnitID = @"";

    // Let the runtime know which UIViewController to restore after taking
    // the user wherever the ad goes and add it to the view hierarchy.
    bannerView_.rootViewController = self;
    [self.view addSubview:bannerView_];

    // Initiate a generic request to load it with an ad.
    [bannerView_ loadRequest:[GADRequest request]];


}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if (self.bannerIsVisible) {
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        banner.frame = CGRectOffset(banner.frame, 0.0, 50.0);
        [UIView commitAnimations];
        self.bannerIsVisible = NO;
        NSLog(@"bannerview did not receive any banner due to %@", error);
        [self callAdMob];

    }
}
- (void)viewDidLoad
{

        [super viewDidLoad];
    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0.0, 367.0);
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    [self.view addSubview:adView];
    adView.delegate = self;
    self.bannerIsVisible = NO;
}

When you run it in the profiler, is the leak listed as GeneralBlock-1024 and GeneralBlock-56? If this is the case, sounds like a UIWebView leak that's been in iOS a while. It seems to be tied to making HTTP requests, or XML requests over HTTP.

Apple should have fixed this in iOS 5 but it still exists in previous versions of iOS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM