0

私は以下のようなコードを書きましたが、次の行でメモリリークが発生しています:

 AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];

私のコードは次のとおりです。

NSError *error;
AVMutableComposition* mixComposition = [AVMutableComposition composition];


NSURL*    audio_inputFileUrl = [_songPath valueForProperty:MPMediaItemPropertyAssetURL];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;

NSString *video_inputFilePath = [documentsDirectory stringByAppendingPathComponent:@"videoOutput1234videoOutput1234.mp4"];
NSLog(@"output url %@",video_inputFilePath);

NSURL*    video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath];


NSArray *docPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath=[docPaths objectAtIndex:0];
NSString* outputFilePath = [docPath stringByAppendingPathComponent:@"OutPut.mov"];//[[NSBundle mainBundle] pathForResource:@"OutPut" ofType:@"mov"];
NSURL*    outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
{
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
}
else {
    NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"OutPut.mov"];
    [[NSFileManager defaultManager] copyItemAtPath:defaultPath toPath:outputFilePath error:&error];
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];
}



CMTime nextClipStartTime = kCMTimeZero;

AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];


AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];

AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];   
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;

if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath]) 
    [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];

[_assetExport exportAsynchronouslyWithCompletionHandler:^(void ) {[self saveVideoToAlbum:outputFilePath]; }       ];    

[outputFileUrl release];
[videoAsset release];
[audioAsset release];
if (_assetExport.status == 2) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Exporting to library is completed" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
    [alertView show];
    [alertView release];
    [mixComposition release];
}

AVASSETEXPORTSESSION をリリースしようとしたときにアプリがクラッシュし、これをリリースする方法がわかりません

答えを教えてください。

4

1 に答える 1

0

解放する必要はありません:

[outputFileUrl release];
[mixComposition release];

これらの行をコードから削除してください。あなたはそれらを所有していない(割り当てられていない)ため、それを行う必要があります。したがって、それらを解放する必要はありません

そして、今はうまくいくはずです。

オートリリース:

AVAssetExportSession* _assetExport = [[[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough] autorelease];

またはリリース:

[_assetExport release];
于 2012-05-16T13:31:52.453 に答える