Do I need What steps to release the GPUImageMovieWriter object? How do I do if I want to use multiple times MovieWriter safely?
Memory leak has occurred in the application using the GPUImage that I have created. A memory leak occurs when output more than twice the video in movieWriter. Debugging using Instruments, came out results that GPUImageMovieWriter has not been released.
Implementation Overview
@property (nonatomic, strong) GPUImageMovieComposition *gpuMovieFileComp;
@property (nonatomic, weak) GPUImageMovieWriter *movieWriter;
@property (nonatomic, weak) GPUImageFilter *exportFilter;
@property (nonatomic, weak) AVMutableComposition *mixComposition;
@property (nonatomic, weak) AVMutableVideoComposition *transformVideoComposition;
@property (nonatomic, weak) AVMutableAudioMix *mutableAudioMix;
_gpuMovieFileComp = [[GPUImageMovieComposition alloc] initWithComposition:self.mixComposition andVideoComposition:self.transformVideoComposition andAudioMix:self.mutableAudioMix];
GPUImageMovieWriter *mr = [[GPUImageMovieWriter alloc]initWithMovieURL:_exportUrl size:CGSizeMake(MOVIE_SIZE_W, MOVIE_SIZE_H)];
[_gpuMovieFileComp enableSynchronizedEncodingUsingMovieWriter:mr];
_movieWriter = mr;
mr = nil;
_movieWriter.shouldPassthroughAudio = YES;
_movieWriter.encodingLiveVideo = NO;
_movieWriter.hasAudioTrack = YES;
_gpuMovieFileComp.playAtActualSpeed = YES;
_gpuMovieFileComp.audioEncodingTarget = _movieWriter;
[_gpuMovieFileComp addTarget:_exportFilter];
[_exportFilter addTarget:_movieWriter];
[_movieWriter setCompletionBlock:^{
weakSelf.gpuMovieFileComp removeTarget:weakSelf.exportFilter];
[weakSelf.exportFilter removeTarget:weakSelf.MovieWriter];
[weakSelf.movieWriter finishRecording];
}];
[_gpuMovieFileComp startProcessing];
[_movieWriter startRecording];
I was confirmed or similar memory leak occurs in SimpleVideoFilter that Sample Program in GPUImage. Add a UIbutton to xib of SimpleVideoFilter, file output method has been modified to fire in the action.
-(IBAction)startBtn:(id)sender
{
NSURL *sampleURL = [[NSBundle mainBundle] URLForResource:@"sample_iPod" withExtension:@"m4v"];
movieFile = [[GPUImageMovie alloc] initWithURL:sampleURL];
movieFile.runBenchmark = YES;
movieFile.playAtActualSpeed = NO;
…
Thus, it is now possible to call multiple times movieWriter. Using the instruments was carried out multiple times file output, it was confirmed the memory leak of GPUImageMovieWriter in the second or later.
Leaking object
movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:movieURL size:CGSizeMake(640.0, 480.0)];
Is this, would be the problem of GPUImageMovieWriter? Or problem on my code? If there is a solution, please let me know. Sorry for my poor English. I’m waiting for advice. Thank you.
Finally, I solved this problem. _movieWriterContext in GPUImageMovieWriter was leaked object. I rewrote the GPUImageMovieWriter.m as follows.
97 // _movieWriterContext = [[GPUImageContext alloc] init];
98 _movieWriterContext = [GPUImageContext sharedImageProcessingContext];
99 // [_movieWriterContext useSharegroup:[[[GPUImageContext sharedImageProcessingContext] context] sharegroup]];
For now , the memory leak no longer occurs ,Crash causes memory pressure also no longer occurs. I hope This solution will help people facing the same problem.