NSData subdataWithRange 问题

发布于 2024-08-02 11:40:20 字数 969 浏览 1 评论 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 ;
    }

每次迭代中的下一个代码中都有内存泄漏,aData(NSData 类)将其保留计数增加 1,并且它发生在 [aData subdataWithRange: NSMakeRange(sO,dS)] 调用之后...我不明白为什么。

i feel like having memory leak in the next code

    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 ;
    }

in each iteration aData which is NSData class increase its retainCount by 1 and it happenes after [aData subdataWithRange: NSMakeRange(sO,dS)] call... i can't understand why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诺曦 2024-08-09 11:40:20

一个可能的原因是每个“copyData”实际上引用了原始数据中的数据。因此,新的数据对象将保留对原始对象的引用。这通常是一个效率优势,因为不需要复制实际数据。 (例外情况是,如果您计划保留一个小的子范围。)

当弹出活动的 NSAutoreleasePool 时,所有数据对象都将被正确释放。

一般来说,您无论如何都不应该查看对象的保留计数。不受您直接控制的代码可以对对象引用执行几乎任何操作,只要它正确平衡其保留和释放即可。如果您担心泄漏,请使用适当的工具,例如 Instruments 的 Leaks 仪器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文