【问题标题】:iOS- How to fix retain cycles that come from modal segues in XCode?iOS-如何修复来自 XCode 中模态序列的保留周期?
【发布时间】:2016-05-16 18:42:02
【问题描述】:

我运行了分配工具,我肯定有一个保留周期,阶梯状的图表是一个死的赠品。但是,我是 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;

这只是代码的一部分,因为其余部分非常庞大,并且基本上是同一行代码,一遍又一遍地对其引用的文件进行了微小的更改。我的问题是我到底该如何修复保留周期?这与我的模态转换有关吗?因为这是导致仪器内存分配疯狂以及我的原生 iOS 设备崩溃的原因。

【问题讨论】:

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


    【解决方案1】:

    在我看来,您确实没有保留周期。听起来您在其他视图控制器之上展示了一系列永无止境的视图控制器。

    使用模态呈现或导航控制器推送具有类似的效果:以前的视图控制器被新的视图控制器覆盖但不会被释放。

    您没有发布任何显示您如何从视图控制器导航到视图控制器的代码,因此很难准确判断您在做什么。

    如果你想从视图控制器 A 到视图控制器 B 到视图控制器 C,然后回到 A,你需要从 A 到 B,然后从 B 到 C,但是为了返回而关闭,而不是呈现视图控制器 A 的新副本。

    【讨论】:

    • 好吧,这也是有道理的。我的 ViewControllers 连接在情节提要中(就像我刚刚单击并将模态连接从按钮拖动到 ViewControllers 一样)。我将如何按照您的建议去做,从 A 到 B,B 到 C,或者编写一些代码来卸载最后一个 ViewController?
    • 永远不要使用模态转场返回。您想使用 unwind segue,或者编写一个 IBAction 方法,通过代码调用dismissViewController:animated:completion:。
    • 感谢您的建议。我会去对我的应用进行更改,如果我还有任何问题,我会回来。
    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多