2012-09-01 12 views
6

Ho un codice che crea CMBlockBuffers e quindi crea un CMSampleBuffer e lo passa a un AVAssetWriterInput.CMBlockBufferCrea gestione memoria

Qual è l'accordo sulla gestione della memoria qui? Secondo la documentazione di Apple, anything you use with 'Create' in the name should be released with CFRelease.

Tuttavia, se uso CFRelease allora la mia app si interrompe con 'malloc: * errore per oggetto 0xblahblah: puntatore viene liberata non è stata assegnata.

CMBlockBufferRef tmp_bbuf = NULL; 
CMBlockBufferRef bbuf = NULL; 
CMSampleBufferRef sbuf = NULL; 
status = CMBlockBufferCreateWithMemoryBlock(
              kCFAllocatorDefault, 
              samples, 
              buflen, 
              kCFAllocatorDefault, 
              NULL, 
              0, 
              buflen, 
              0, 
              &tmp_bbuf); 

if (status != noErr || !tmp_bbuf) { 
    NSLog(@"CMBlockBufferCreateWithMemoryBlock error"); 
    return -1; 
} 
// Copy the buffer so that we get a copy of the samples in memory. 
// CMBlockBufferCreateWithMemoryBlock does not actually copy the data! 
// 
status = CMBlockBufferCreateContiguous(kCFAllocatorDefault, tmp_bbuf, kCFAllocatorDefault, NULL, 0, buflen, kCMBlockBufferAlwaysCopyDataFlag, &bbuf); 
//CFRelease(tmp_bbuf); // causes abort?! 
if (status != noErr) { 
    NSLog(@"CMBlockBufferCreateContiguous error"); 
    //CFRelease(bbuf); 
    return -1; 
} 


CMTime timestamp = CMTimeMake(sample_position_, 44100); 

status = CMAudioSampleBufferCreateWithPacketDescriptions(
    kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1, timestamp, NULL, &sbuf); 

sample_position_ += n; 
if (status != noErr) { 
    NSLog(@"CMSampleBufferCreate error"); 
    return -1; 
} 
BOOL r = [audioWriterInput appendSampleBuffer:sbuf]; // AVAssetWriterInput 
//memset(&audio_buf_[0], 0, buflen); 
if (!r) { 
    NSLog(@"appendSampleBuffer error"); 
} 
//CFRelease(bbuf); 
//CFRelease(sbuf); 

Quindi, in questo codice, dovrei essere usando CFRelease su qualsiasi cosa?

+0

Hai mai questo capito? – kevlar

+0

@kevlar Ho finito per modificare tutto per fare la compressione asincrona usando GCD. Avevo bisogno di liberare quei buffer. Non sono ancora sicuro del motivo per cui questo si è bloccato. Se temi di avere perdite di memoria, profila il tuo codice usando Strumenti (parte di XCode). – Pete

risposta

8

La chiave è il parametro blockAllocator a CMBlockBufferCreateWithMemoryBlock:

Allocatore da utilizzare per l'assegnazione della memoryBlock, se memoryBlock è NULL. Se memoryBlock non è NULL, questo allocatore verrà utilizzato per deallocarlo se fornito. Passando NULL si utilizzerà l'allocatore predefinito (come impostato al momento della chiamata). Passa kCFAllocatorNull se non si desidera deallocazione.

Dal momento che non desidera che i "campioni" da deallocate quando si rilascia il CMBlockBuffer, si desidera passare a kCFAllocatorNull come il 4 ° parametro in questo modo:

status = CMBlockBufferCreateWithMemoryBlock(
              kCFAllocatorDefault, 
              samples, 
              buflen, 
              kCFAllocatorNull, 
              NULL, 
              0, 
              buflen, 
              0, 
              &tmp_bbuf); 
0
CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1, timestamp, NULL, &sbuf); 

questo dovrebbe essere:

CMAudioSampleBufferCreateWithPacketDescriptions(kCFAllocatorDefault, bbuf, TRUE, 0, NULL, audio_fmt_desc_, 1024, timestamp, NULL, &sbuf); 
+0

Cura di espandere perché questo si riferisce alla gestione della memoria e CFRelease? – Pete