`initWithNibName` 内存泄漏

Memory leak on `initWithNibName`

我遇到内存泄漏问题。在 Instruments 中,我在这条线上遇到内存泄漏:

VDLPlaybackViewController *videoController = [[VDLPlaybackViewController alloc] initWithNibName:@"VDLPlaybackView" bundle:nil];

我不确定这可能是什么问题。这是 VDLPlaybackViewController.h:

的头文件
@protocol VDLPlaybackViewControllerDelegate <NSObject>
@required
-(void)playerShouldClose;
@end

@interface VDLPlaybackViewController : UIViewController <UIDocumentInteractionControllerDelegate>


@property (nonatomic, strong) id<VDLPlaybackViewControllerDelegate> delegate;

// content file stuff.
@property (nonatomic, strong) File *file;
@property (nonatomic, strong) NSNumber *contentID;
@property (nonatomic, strong) NSURL *fileURL;
@property (nonatomic, strong) NSString *pageTitle;

// layout stuff
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topTimeViewHeight;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bottomControlViewHeight;

@property (nonatomic, strong) IBOutlet UIView *movieView;
@property (nonatomic, strong) IBOutlet UIView *navBarView;
@property (nonatomic, strong) IBOutlet UISlider *positionSlider;
@property (nonatomic, strong) IBOutlet UIButton *timeDisplay;
@property (nonatomic, strong) IBOutlet UIButton *playPauseButton;
@property (nonatomic, strong) IBOutlet UIButton *subtitleSwitcherButton;
@property (nonatomic, strong) IBOutlet UIButton *audioSwitcherButton;
@property (nonatomic, strong) IBOutlet UIButton *screenSizeSwitcherButton;
//@property (nonatomic, strong) IBOutlet UINavigationBar *toolbar;
@property (nonatomic, strong) IBOutlet UIView *controllerPanel;
@property (nonatomic, strong) IBOutlet UISlider *volumeSlider;
@property (nonatomic, strong) IBOutlet MPVolumeView *volumeView;
@property (nonatomic, strong) IBOutlet MPVolumeView *airplayView;

@property (nonatomic, assign) CGRect shareButtonFrame;
@property (nonatomic, strong) MultiFileShareButtonController *shareButtonController;
@property (nonatomic, strong) FavoriteFileButtonView *favoriteButtonController;
@property (nonatomic, strong) UIDocumentInteractionController *interactionController;
@property (nonatomic, assign) BOOL isDestroying;

- (void)setMediaURL:(NSURL*)theURL;

- (IBAction)closePlayback:(id)sender;

- (IBAction)positionSliderDrag:(id)sender;
- (IBAction)positionSliderAction:(id)sender;
- (IBAction)toggleTimeDisplay:(id)sender;

- (IBAction)playandPause:(id)sender;
//- (IBAction)switchAudioTrack:(id)sender;
//- (IBAction)switchSubtitleTrack:(id)sender;
- (IBAction)switchVideoDimensions:(id)sender;

@end

有人知道是什么原因造成的吗?

检测工具告诉您以下行存在泄漏:

VDLPlaybackViewController *videoController = [[VDLPlaybackViewController alloc] initWithNibName:@"VDLPlaybackView" bundle:nil];

这并不意味着只是这一行导致泄漏,该工具告诉您此变量的引用作为泄漏存在。

您应该寻找传递了对 VDLPlaybackViewController *videoController 的强引用的实例。可能是作为委托或在完成块中。

你问题中的界面有点问题。应该弱而不是强

@property (nonatomic, weak) id<VDLPlaybackViewControllerDelegate> delegate;

找到更多这样的例子,你已经VDLPlaybackViewController作为一个强大的参考委托,你将能够解决问题。

更多地了解泄漏实际发生的原因。经过 https://cocoacasts.com/what-are-strong-reference-cycles