简体   繁体   English

Swift静态数组-未分配要释放的指针

[英]Swift Static Array - pointer being freed was not allocated

I have a static array metricsTransactionData in ServiceRequest class. 我在ServiceRequest类中有一个静态数组metricsTransactionData。 I am invoking multiple request continuously and sometimes I get this error "error for object 0x10b874cb0: pointer being freed was not allocated" on this line ServiceRequest.metricsTransactionData.append(samp). 我连续调用多个请求,有时在此行ServiceRequest.metricsTransactionData.append(samp)上收到此错误“对象0x10b874cb0错误:未分配释放的指针”。 Pls help 请帮助

public func urlSession(_ session: URLSession, task: URLSessionTask, didFinishCollecting metrics: URLSessionTaskMetrics) {
    for mem in metrics.transactionMetrics {
        var samp = MetricsData()
        samp.requestDate = mem.requestStartDate
        samp.responseDate = mem.responseEndDate
        samp.url = mem.request.url?.absoluteString
        ServiceRequest.metricsTransactionData.append(samp)
    }
}

Stack trace: 堆栈跟踪:

* thread #3, queue = 'NSOperationQueue 0x6000001edb20 (QOS: UNSPECIFIED)', stop reason = signal SIGABRT
    frame #0: 0x0000000106897b66 libsystem_kernel.dylib`__pthread_kill + 10
    frame #1: 0x00000001068d1080 libsystem_pthread.dylib`pthread_kill + 333
    frame #2: 0x0000000106644c45 libsystem_c.dylib`abort + 127
    frame #3: 0x00000001067986e4 libsystem_malloc.dylib`malloc_vreport + 545
    frame #4: 0x00000001067988d5 libsystem_malloc.dylib`malloc_report + 152
    frame #5: 0x00000001049bd910 libswiftCore.dylib`_swift_release_dealloc + 16
    frame #6: 0x0000000104980017 libswiftCore.dylib`assignWithTake value witness for Swift.Array + 23
    frame #7: 0x00000001046f3a5a libswiftCore.dylib`(extension in Swift):Swift._ArrayBufferProtocol._arrayOutOfPlaceUpdate<A where A1: Swift._PointerFunction, A.Element == A1.Element>(inout Swift._ContiguousArrayBuffer<A.Element>, Swift.Int, Swift.Int, A1) -> () + 1146
    frame #8: 0x000000010470aa11 libswiftCore.dylib`Swift.Array._copyToNewBuffer(oldCount: Swift.Int) -> () + 209
    frame #9: 0x000000010470aaf0 libswiftCore.dylib`Swift.Array._makeUniqueAndReserveCapacityIfNotUnique() -> () + 192
    frame #10: 0x000000010470ad98 libswiftCore.dylib`Swift.Array.append(A) -> () + 24

The error tells you that it is a very bad idea to use a static/global array that will be updated from various background tasks. 该错误告诉您,使用将要从各种后台任务更新的静态/全局数组是一个非常糟糕的主意。 Swift arrays are not thread-safe. Swift数组不是线程安全的。

Means that your array gets resized by one task while another might already do the same, resulting in a bad memory access. 这意味着您的数组将由一项任务调整大小,而另一项可能已经执行了相同的任务,从而导致错误的内存访问。 Likely calling (at least) append in a DispatchQueue.main.async would fix the problem if you really have to do it that way, but if you want to maintain the order, you would need to handle synchronization yourself. 如果确实必须这样做,则可以(至少)在DispatchQueue.main.async中调用append可以解决此问题,但是,如果您要保持顺序,则需要自己处理同步。

But in any way i strongly recommend that you find another solution that prevents that kind of write access to the array directly. 但是无论如何,我强烈建议您找到另一种解决方案,以防止直接对数组进行这种写访问。 Globally writable arrays are bad. 全局可写数组是不好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM