0

我创建了我的班级的一个例子:https ://github.com/ChoadPet/H.264-Decoding

当我使用配置构建我的应用程序时,DEBUG一切正常,但是当我归档RELEASE它时,它在这条线上崩溃:

let status = VTDecompressionSessionDecodeFrame(session,
                                               sampleBuffer: sampleBuffer,
                                               flags: defaultDecodeFlags,
                                               frameRefcon: nil,
                                               infoFlagsOut: nil)

Address Sanitizer启用后,我收到此错误 :Thread 9: Use of deallocated memory

SUMMARY: AddressSanitizer: heap-use-after-free
(.../Frameworks/libclang_rt.asan_ios_dynamic.dylib:arm64+0x1a1f4) in wrap_memmove
...
(if you need more crash info, let me know)

没有:Thread 12: EXC_BAD_ACCESS (code=1, address=0x107dd0020)


我确实知道有一些内存被释放并通过VTDecompressionSessionDecodeFrame方法访问,但我找不到任何十六进制地址,我不明白这如何与DEBUG构建完美配合。
在此方法之前,sessionsampleBuffer已成功创建(初始化)。

是否有一些我可以更改的项目设置DEBUG configuration可能导致崩溃?或者有人可以指出我的代码问题?

谢谢!

4

2 回答 2

1

将 Release 存档更改Optimization Level为与 Debug 相同 -No Optimization[-Onone]隐藏问题,但更改构建配置不是解决此类问题的正确方法。此外,问题并不完全在sampleBuffer. 问题出在blockBufferOut参数上,sampleBuffer稍后再讨论。我将更新 repo 源代码,以便社区可以清楚地看到更改。

所以我之前有这个逻辑:

// 1. Creating blockBuffer from `bufferPointer`
localFrame.withUnsafeMutableBufferPointer { bufferPointer in
   // I should write everything in this body,
   // since bufferPointer would be released after the closure
   // and so, it will be released from bufferBlock
}

// 2. I called this method, which is creating `sampleBuffer`
CMSampleBufferCreateReady

// 3. I called this method, which is decode frame with session and sampleBuffer 
VTDecompressionSessionDecodeFrame

/* 
and on this line, it crashes with EXC_BAD_ACCESS, 
because the sample wasn't valid anymore, because 
on step 1, bufferPointer only valid inside body closure, 
so it's release after block when I created sampleBuffer
and later decode it.


This is even pointed by documentation:

Parameters

body
Closure with an UnsafeMutableBufferPointer parameter that points to the
contiguous storage for the array. If no such storage exists, it is created. If
the body has a return value, that value is also used as the return value for the
withUnsafeMutableBufferPointer(_:) method. The pointer argument is valid only for
the duration of the method’s execution.
*/

总结:如果你对bufferPointer进行操作,所有的操作都在一个闭包中。

于 2020-08-03T10:22:48.897 回答
0

添加到@vpoltave 提供的答案EXC_BAD_ACCESS中,我发现在创建样本缓冲区后使用调度队列会给我一个错误,或者如果你打开地址清理器,那么它会在com.apple.coremedia.videomediaconverterwith中出错Use of deallocated memory。建议将原始数据排入队列,而不是创建的缓冲区。对我来说,来源是回调中的基本格式的框架。

于 2021-08-27T08:49:30.813 回答