Pergunta

I have an instance of AVPlayer in my application. I use the time boundary observing feature:

[self setTimeObserver:[player addBoundaryTimeObserverForTimes:watchedTimes
    queue:NULL usingBlock:^{
        NSLog(@"A: %i", [timeObserver retainCount]);
        [player removeTimeObserver:timeObserver];
        NSLog(@"B: %i", [timeObserver retainCount]);
        [self setTimeObserver:nil];
    }]];

The problem is that according to Instruments I am leaking some arrays and values somewhere around this code. I checked the retain count of the time-observing token returned by AVPlayer on places marked A and B in the sample code. At the A point the retain count is 2, at point B the retain count increases to 3 (!). Adding a local autorelease pool does not change anything. I know that retain count is not a reliable metric, but this seems to be fishy. Any ideas about why the retain count increases or about my leaks? The stack trace at the leak point looks like this:

   0 libSystem.B.dylib calloc
   1 libobjc.A.dylib _internal_class_createInstanceFromZone
   2 libobjc.A.dylib class_createInstance
   3 CoreFoundation __CFAllocateObject2
   4 CoreFoundation +[__NSArrayI __new::]
   5 CoreFoundation -[__NSPlaceholderArray initWithObjects:count:]
   6 CoreFoundation +[NSArray arrayWithObjects:count:]
   7 CoreFoundation -[NSArray sortedArrayWithOptions:usingComparator:]
   8 CoreFoundation -[NSArray sortedArrayUsingComparator:]
   9 AVFoundation -[AVPlayerOccasionalCaller initWithPlayer:times:queue:block:]
  10 AVFoundation -[AVPlayer addBoundaryTimeObserverForTimes:queue:usingBlock:]

If I understand things correctly, AVPlayerOccasionalCaller is the “opaque” object returned by addBoundaryTimeObserverForTimes:queue:usingBlock:, or the time observer.

Foi útil?

Solução

Do not use -retainCount.

The absolute retain count of an object is meaningless.

You should call release exactly same number of times that you caused the object to be retained. No less (unless you like leaks) and, certainly, no more (unless you like crashes).

See the Memory Management Guidelines for full details.


In this specific case, the retain count you are printing is entirely irrelevant. removeTimeObserver: is probably retaining and autoreleasing the object. Doesn't really matter; it is an implementation detail.

When using the Leaks template in Instrument, note that the Allocations instrument is configured to record reference counts. When you have detected a "leak", look at the list of reference count events for that object. There will likely be a stack where some code of yours is triggering an extra retain. If not, it might be a framework bug.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top