iOS-如何修复来自 XCode 中模态转场的保留周期?

标签 ios objective-c xcode memory-management memory-leaks

我运行了分配工具,我肯定有一个保留周期,阶梯状的图表是一个死的赠品。但是,我是 iOS 编程的新手,甚至在通过互联网筛选文档后也不知道如何解决我的保留周期问题。据我所知,有一些强引用阻止未使用的内存在我的 ARC 项目中被释放。该应用程序是一个简单的音板,但内容很多,这就是为什么我以前不必处理这个问题。内存现在是一个真正的问题。对于 ViewController.m,我有

@import GoogleMobileAds;

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController () <GADInterstitialDelegate, UIAlertViewDelegate>
{
    AVAudioPlayer *_audioPlayer;
}
@property(nonatomic, strong) GADInterstitial *interstitial;

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createAndLoadInterstitial];

    BannerManager *sharedManager = [BannerManager sharedManager];
    GADBannerView* bView = [sharedManager setupBannerAds:self.view];
    bView.delegate = self;
    bView.rootViewController = self;

    GADRequest *request = [GADRequest request];
    [bView loadRequest:request];
}

- (void)adViewDidReceiveAd:(GADBannerView *)bannerView {
    NSLog(@"adViewDidReceiveAd: %@", bannerView.adNetworkClassName);
}


int counter = 0;

#pragma Interstitial button actions

- (IBAction)playAgain:(id)sender {

    if (counter % 15 == 0) {
        if (self.interstitial.isReady) {
            [self.interstitial presentFromRootViewController:self];
        } else {

        }
    }

    counter++;
}

- (void)createAndLoadInterstitial {
    self.interstitial =
    [[GADInterstitial alloc] initWithAdUnitID:@"mygoogleidgoeshere"];
    self.interstitial.delegate = self;

    GADRequest *request = [GADRequest request];

    [self.interstitial loadRequest:request];
}


#pragma mark GADInterstitialDelegate implementation

- (void)interstitial:(GADInterstitial *)interstitial
didFailToReceiveAdWithError:(GADRequestError *)error {
    NSLog(@"interstitialDidFailToReceiveAdWithError: %@", [error localizedDescription]);
}

- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
    NSLog(@"interstitialDidDismissScreen");
    [self createAndLoadInterstitial];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





- (IBAction)areyoufinishedyet:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"areyoufinishedyet"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];

}

- (IBAction)areyoulooking:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"areyoulooking"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)blueeyes:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"blueeyes"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)crosshairs:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"crosshairs"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)doyoumind:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"doyoumind"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)dontblink:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"dontblink"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)growontrees:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"growontrees"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)iamagenius:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"iamagenius"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];}

- (IBAction)imarealboy:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"imarealboy"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)importantbusiness:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"importantbusiness"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)inmyeye:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"inmyeye"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)justblinked:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"justblinked"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)mustyoureally:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"mustyoureally"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)nottheenemy:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"nottheenemy"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)oddlyenough:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"oddlyenough"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];}

- (IBAction)staringcontest:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"staringcontest"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];
}

- (IBAction)youreaklutz:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"youreaklutz"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];}

- (IBAction)youreamoron:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"youreamoron"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];}

- (IBAction)yourepissingmeoff:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"yourepissingmeoff"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];}

- (IBAction)challengeme:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"challengeme"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];}

- (IBAction)forerunners:(id)sender {

    soundFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                        pathForResource:@"forerunners"
                                        ofType:@"mp3"]];

    sound = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFile error:nil];
    sound.delegate = self;
    [sound play];}

对于 Viewcontroller.h 我有

#import <UIKit/UIKit.h>
#import "BannerManager.h"
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>

@import GoogleMobileAds;

@interface ViewController: UIViewController<GADBannerViewDelegate, AVAudioPlayerDelegate>  {
    NSURL *soundFile;
    AVAudioPlayer *sound;
}

//343 GUILTY SPARK
- (IBAction)areyoufinishedyet:(id)sender;
- (IBAction)areyoulooking:(id)sender;
- (IBAction)blueeyes:(id)sender;
- (IBAction)crosshairs:(id)sender;
- (IBAction)doyoumind:(id)sender;
- (IBAction)dontblink:(id)sender;
- (IBAction)growontrees:(id)sender;
- (IBAction)iamagenius:(id)sender;
- (IBAction)imarealboy:(id)sender;
- (IBAction)importantbusiness:(id)sender;
- (IBAction)inmyeye:(id)sender;
- (IBAction)justblinked:(id)sender;
- (IBAction)mustyoureally:(id)sender;
- (IBAction)nottheenemy:(id)sender;
- (IBAction)oddlyenough:(id)sender;
- (IBAction)staringcontest:(id)sender;
- (IBAction)youreaklutz:(id)sender;
- (IBAction)youreamoron:(id)sender;
- (IBAction)yourepissingmeoff:(id)sender;

//ARBITER
- (IBAction)challengeme:(id)sender;
- (IBAction)forerunners:(id)sender;
- (IBAction)halospurpose:(id)sender;
- (IBAction)imgoingtocutit:(id)sender;
- (IBAction)imprepared:(id)sender;
- (IBAction)readytofight:(id)sender;
- (IBAction)sacredrings:(id)sender;
- (IBAction)saveyouranger:(id)sender;
- (IBAction)youshotmefoo:(id)sender;
- (IBAction)tararusstop:(id)sender;
- (IBAction)trymypatience:(id)sender;
- (IBAction)whatyouwant:(id)sender;
- (IBAction)arbiterdo:(id)sender;

//BRUTES
- (IBAction)adaysrations:(id)sender;
- (IBAction)atinymorsel:(id)sender;
- (IBAction)bastardelite:(id)sender;
- (IBAction)boilinyourpot:(id)sender;
- (IBAction)cannedmeat:(id)sender;
- (IBAction)cannotescape:(id)sender;

这只是代码的一部分,因为其余部分相当庞大,并且基本上是同一行代码,只是对其引用的文件进行了微小的更改。我的问题是我究竟该如何修复保留周期?这与我的模态转换有关吗?因为这就是导致仪器内存分配异常和我的 native iOS 设备崩溃的原因。

最佳答案

在我看来,您确实没有保留周期。听起来您正在其他 View Controller 之上呈现一系列永无止境的 View Controller 。

使用模态呈现或导航 Controller 推送具有类似的效果:先前的 View Controller 被新 View Controller 覆盖但未被释放。

您没有发布任何显示如何从 View Controller 导航到 View Controller 的代码,因此很难说出您到底在做什么。

如果你想从 View Controller A 到 View Controller B 再到 View Controller C,然后返回到 A,你需要从 A 到 B,然后从 B 到 C,但是为了返回而关闭,而不是呈现 View Controller A 的新副本。

关于iOS-如何修复来自 XCode 中模态转场的保留周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260995/

相关文章:

iphone - 将 UIButton 添加到 UItableview 单元格

ios - 我什么时候应该重新加载数据()?

iphone - 如何判断plist中是否包含NSDictionary或NSMutableArray?

ios - 点击 UIButton 并在下一个查看表格单元格行选择状态时调用它的操作?

ios - Xcode 中的内存警告 - didReceiveMemoryWarning 中的作用是什么?

swift - If Else 语句与选择器的链接

ios - 将 Char 或 NSString 添加到 Objective C 枚举中

iphone - 在 View IOS 中禁用用户交互

iphone - 更改 navigationItem.title 的位置

ios - UIPageViewController 在方向改变时抛出 SIGABRT