0

次のコードでメモリ リークが発生しているような気がします

    while([outData length] + ptr[currentPacket].mDataByteSize < inBytesToGet && currentPacket < packetsCount)
    {
        NSLog(@" ++> %d", [aData retainCount]) ;
        NSInteger sO = ptr[currentPacket].mStartOffset ;
        NSInteger dS = ptr[currentPacket].mDataByteSize ;
        NSLog(@"     get: cP: %d tP: %d mStartOffset: %d mDataByteSize: %d", currentPacket, packetsCount, sO, dS) ;
        NSData *copyRange = [aData subdataWithRange: NSMakeRange(sO,dS)] ;
        NSLog(@" => %d", [aData retainCount]) ;
        [outData appendData:copyRange] ;
        ptr[currentPacket].mStartOffset = bytesFilled + inOffset ;
        [outPackets appendBytes: &ptr[currentPacket] length: sizeof(AudioStreamPacketDescription)] ;
        currentPacket++ ;
        bytesFilled += dS ;
    }

NSData クラスである aData の反復ごとに、retainCount が 1 増加し、[aData subdataWithRange: NSMakeRange(sO,dS)] 呼び出しの後に発生します...理由がわかりません。

4

1 に答える 1

3

A likely reason is that each “copyData” is actually referencing the data in the original. As such, the new data objects will keep a reference to the original object. This is generally an efficiency advantage, since no copy of the actual data needs to be made. (The exception would be if you planned to keep a small subrange around.)

All of the data objects will be released properly when the active NSAutoreleasePool is popped.

In general, you shouldn’t be looking at the retain count of objects anyway. Code that isn’t under your direct control can do just about whatever it wants with object references, as long as it balances its retains and releases properly. If you’re concerned about leaks, use appropriate tools such as Instruments’s Leaks instrument.

于 2009-08-14T22:02:21.417 に答える