Reputation: 3321
I am noticing that my AVAssetReader's (reader in this case) retainCount continues to rise with this code. I believe I am properly releasing everything I should. Is there a way to check a CMSampleBufferRef ref count? Does anyone see something I forgetting to release?
AVAssetReaderTrackOutput* trackOutput = (AVAssetReaderTrackOutput*)[reader.outputs objectAtIndex:0];
NSLog(@"PreReader Count: %d", reader.retainCount);
CMSampleBufferRef sampleBufferRef = [trackOutput copyNextSampleBuffer];
NSLog(@"Reader Count: %d", reader.retainCount); //retainCount has increased by 1
if (sampleBufferRef) {
CopySampleBufferToStream(sampleBufferRef, stream);
CMSampleBufferInvalidate(sampleBufferRef);
CFRelease(sampleBufferRef); //Retain count doesn't go back down, item hasn't been deleted?
}
With this as the key parks of CopySampleBufferToStream
void CopySampleBufferToStream(CMSampleBufferRef sampleBufferRef, cSoundStream* stream)
{
CMBlockBufferRef blockBufferRef = CMSampleBufferGetDataBuffer(sampleBufferRef);
size_t length = CMBlockBufferGetDataLength(blockBufferRef);
do
{
//...
OSStatus result = CMBlockBufferCopyDataBytes(blockBufferRef, startOffset, copyLength, (stream->mSrcBuffers[stream->mActiveWriteIdx].mData + stream->mSrcBuffers[stream->mActiveWriteIdx].mBufferOffset));
//...
} while(stream->ContinueLoading() && stream->TransitionNotReady() && copyLength < length);
}
Edit:
So after further debugging I was able to confirm that after the loop exits and the Autorelease pool is cleared the AVAssetReader does get down to a retainCount of 1
I moved the pool release before the reader release to test this...
[pool release];
NSLog(@"Reader Count: %d", reader.retainCount);
[reader release];
and I got the expected result of a retainCount of 1.
However memory still appears to be leaking from the CMSampleBufferRefs as FigSampleBuffer's are staying around. Here is what I found in Instruments:
and the call stack leading to the CMSampleBufferRef...
This was after I have exited the loop, cleaned everything up, then recreated it for another AVAsset. As I repeat this more FigSampleBuffer's appear in each headshot.
Upvotes: 2
Views: 1543
Reputation: 162712
Don't call retainCount
; it is useless.
There isn't anything obviously wrong with that code, memory management wise.
The real question is; Have you balanced all retains with releases? If so, you are done.
If you have and you see unbounded memory growth, then file a bug.
The absolute retainCount at any given time is an implementation detail and that value is likely to reflect internal implementation details way beyond your control. Furthermore, the retainCount never reflects whether or not an object is currently in an autorelease pool.
The only time the retainCount can be absolutely known is:
Upvotes: 1