Swift: UnsafeMutablePointer.deinitialize fatal error with negative count when appending to array

家住魔仙堡 提交于 2019-12-03 15:20:21

问题


The code below generates this error (appending to exporters):

fatal error: UnsafeMutablePointer.deinitialize with negative count

    var exporters = [AVAssetExportSession]()

    let exporter = AVAssetExportSession(asset: mainComposition, presetName: AVAssetExportPresetHighestQuality)!
    exporter.videoComposition = videoComposition
    exporter.outputFileType = AVFileTypeMPEG4
    exporter.outputURL = exportURL
    exporter.shouldOptimizeForNetworkUse = true
    exporters.append(exporter)

The other posts on StackOverflow regarding UnsafeMutablePointer.deinitialize do not shed much light on the issue, which doesn't happen consistently.

Any ideas?


回答1:


I had a similar error, the issue was caused by multiple threads modifying the array at the same time. Wrapping the append calls in a serial dispatch queue solved it for me.

    let serialQueue = DispatchQueue(label: "myqueue")

    serialQueue.sync {
        exporters.append(exporter)
    }


来源:https://stackoverflow.com/questions/40080508/swift-unsafemutablepointer-deinitialize-fatal-error-with-negative-count-when-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!