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

2

26

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?

Echoechoic answered 17/10, 2016 at 7:17 Comment(0)
B
40

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)
    }
Banzai answered 14/2, 2017 at 15:27 Comment(0)
G
3

I have solved this type of issue like this way:

DispatchQueue.global(qos: .background).sync {
        //your code 
    }
Grandnephew answered 12/3, 2020 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.