繁体   English   中英

从 NSDataAsset 读取时偶尔崩溃

[英]Occasional crashes while read from NSDataAsset

我的一些用户报告了崩溃。 我不确定我的想法是否正确,所以我显示了代码和带有错误描述的崩溃报告部分,也许有人对我有帮助。

用例:资产目录中的文件包含多个国家/地区边界线的坐标。 里面还有一些字符串。 此代码片段读取文件并将转换后的数据存储到变量中。

有时应用程序会在代码片段的最后一行崩溃(我每周收到 1 或 2 个报告)(让 numberOfCountries = Int(dataReadBufferUInt64))。 我有一组 beta 测试人员,他们完全没有问题。 所以这是一个令人沮丧的问题。

我认为这与我如何从文件中读取数据有关。 也许我必须使用另一种方式?

欢迎任何帮助/评论!

数据结构:

资产是以编程方式生成的文件。 代码片段正在处理此文件的标头。

第一个元素是版权字符串,其中包含文件版本、创建日期等一些信息。

字符串有两个元素:1)字符串的长度作为 UInt16 值(范围 0 ... UInt16.max) 2)字符串元素(如果长度 > 0),每个“字符”作为一个 UInt16 值

第二个元素是该文件中的国家/地区数。 它是一个 UInt64 值。

其余的数据结构相当复杂,但在这里不相关,因为文件是按顺序读取的,如果它崩溃,它会在读取/转换 UInt64 时崩溃。

只有一个版本的文件“out in the wild”。 每次用户界面建立时,每个应用程序都会读取此文件。 这就是为什么我不明白偶尔崩溃的原因......

代码片段:

// calculate the size of a record, we use "size" as this is the number of bytes used to store on record
// other possibilities:
// .stride = number of bytes used to store one record and the added nul bytes to align to next memory bounds
// .alignment = number of bytes of alignment bounds
let sizeUInt16 : UInt64 =               UInt64(MemoryLayout<UInt16>.size)
let sizeUInt64 : UInt64 =               UInt64(MemoryLayout<UInt64>.size)



// get the data out of the asset catalog
if let countryBorderLineData = NSDataAsset(name: "CountryBorderLine data", bundle: Bundle.main)?.data {

    // the read buffers, one for each expected data type
    var dataReadBufferUInt16 : UInt16 = 0
    var dataReadBufferUInt64 : UInt64 = 0


    // read the string with the entry comment
    // read the length of the string
    (countryBorderLineData as NSData).getBytes(&dataReadBufferUInt16, range: NSRange(location: nextLocation,
                                                                      length: Int(sizeUInt16) ))
    // advance the pointer
    nextLocation += Int(sizeUInt16)

    // take the number of items we should read
    var numberOfItemsToRead : Int = Int(dataReadBufferUInt16)

    // check if this is not an empty string
    if numberOfItemsToRead > 0 {

       // target buffer of the string
       var UTF16Array : [UInt16] = []

       // loop to read all content
       for _ in 0 ..< numberOfItemsToRead {

            // read next string element
            (countryBorderLineData as NSData).getBytes(&dataReadBufferUInt16, range: NSRange(location: nextLocation,
                                                                              length: Int(sizeUInt16) ))
            // advance the pointer
            nextLocation += Int(sizeUInt16)

            // append read string element to the array

            UTF16Array.append(dataReadBufferUInt16)
        }

        // convert the read array into a string
        let resultString = String(utf16CodeUnits: UTF16Array, count: UTF16Array.count)

    }

    // read the number of countries
    (countryBorderLineData as NSData).getBytes(&dataReadBufferUInt64, range: NSRange(location: nextLocation,
                                                                                 length: Int(sizeUInt64) ))
    // advance the pointer
    nextLocation += Int(sizeUInt64)

    // This line SOMETIMES crashes (see crash subset of crash report)
    let numberOfCountries = Int(dataReadBufferUInt64)

    ...

}

崩溃报告的这部分显示错误

Date/Time:           2019-08-28 22:00:06.5042 +0200
Launch Time:         2019-08-28 22:00:02.2638 +0200
OS Version:          iPhone OS 12.4 (16G77)
Baseband Version:    1.06.02
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x8000000000000010
VM Region Info: 0x8000000000000010 is not in any region.  Bytes after     previous region: 9223372025580486673  
  REGION TYPE                      START - END             [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
  MALLOC_NANO            0000000280000000-00000002a0000000 [512.0M] rw-/rwx SM=PRV  
--->  
  UNUSED SPACE AT END

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [22974]
Triggered by Thread:  3

此类的所有方法都在以下 GCD 队列中调用:

let myQueueForBorderLines : DispatchQueue = DispatchQueue(
label: "appName.myQueueForBorderLines", qos: .userInitiated)

使用的数据结构等仅由此类方法读取和管理,因此我认为这不是多线程问题。

更新

知道旧代码中的numberOfBytesToRead不会是问题的原因,我现在可以向您展示Data的推荐用法。

let sizeUInt16 = MemoryLayout<UInt16>.size
let sizeUInt64 = MemoryLayout<UInt64>.size

// get the data out of the asset catalog
if let countryBorderLineData = NSDataAsset(name: "CountryBorderLine data", bundle: Bundle.main)?.data {
    var nextLocation = 0

    // the read buffers, one for each expected data type
    var dataReadBufferUInt16: UInt16 = 0
    var dataReadBufferUInt64: UInt64 = 0

    // read the string with the entry comment
    // read the length of the string
    _ = withUnsafeMutableBytes(of: &dataReadBufferUInt16) {bufPtr in
        countryBorderLineData.copyBytes(to: bufPtr, from: nextLocation...)
    }
    // advance the pointer
    nextLocation += Int(sizeUInt16)

    // take the number of items we should read
    let numberOfItemsToRead = Int(dataReadBufferUInt16)

    // check if this is not an empty string
    if numberOfItemsToRead > 0 {

        // target buffer of the string
        var utf16Array: [UInt16] = Array(repeating: 0, count: numberOfItemsToRead)

        utf16Array.withUnsafeMutableBufferPointer {bufPtr in
            countryBorderLineData.copyBytes(to: bufPtr, from: nextLocation...)
        }
        // advance the pointer
        nextLocation += numberOfItemsToRead * sizeUInt16

        // convert the read array into a string
        let resultString = String(utf16CodeUnits: utf16Array, count: utf16Array.count)
        print(resultString)
    }

    // read the number of countries
    _ = withUnsafeMutableBytes(of: &dataReadBufferUInt64) {bufPtr in
        countryBorderLineData.copyBytes(to: bufPtr, from: nextLocation...)
    }
    // advance the pointer
    nextLocation += sizeUInt64

    // This line SOMETIMES crashes (see crash subset of crash report)
    let numberOfCountries = Int(dataReadBufferUInt64)

    //...

}

Swift.Data的实现最近发生了变化,所以它可能会导致一些问题,但可能性不高(几乎不太可能)。

如果您的代码在多线程上下文中运行,则可能会导致有时崩溃

无论如何,当您显示更多上下文时,我会检查它并再次更新我的答案。


旧答案

这取决于您的组织方式(您应该更好地显示 NSDataAsset 的规范),但是您的代码使用numberOfBytesToRead * sizeUInt16字节,此循环:

       // loop to read all content
       for _ in 0 ..< numberOfBytesToRead {

            //...

            // advance the pointer
            nextLocation += Int(sizeUInt16)

            //...
        }

在此循环之后, nextLocation可能指向某个未知位置,这可能: - 超出countryBorderLineData的有效范围 - 导致Int(dataReadBufferUInt64)溢出 - ...


并且循环遍历每个 UTF-16 代码点并不是读取 UTF-16 字符串的有效方法。

我会将您的代码重新编写为:

let sizeUInt16 = MemoryLayout<UInt16>.size
let sizeUInt64 = MemoryLayout<UInt64>.size

if let countryBorderLineData = data {
    var nextLocation = 0

    // the read buffers, one for each expected data type
    var dataReadBufferUInt16: UInt16 = 0
    var dataReadBufferUInt64: UInt64 = 0

    // read the string with the entry comment
    // read the length of the string
    _ = withUnsafeMutableBytes(of: &dataReadBufferUInt16) {bufPtr in
        countryBorderLineData.copyBytes(to: bufPtr, from: nextLocation...)
    }
    // advance the pointer
    nextLocation += sizeUInt16

    // take the number of bytes we should read
    let numberOfBytesToRead = Int(dataReadBufferUInt16)

    // check if this is not an empty string
    if numberOfBytesToRead > 0 {
        assert(numberOfBytesToRead.isMultiple(of: sizeUInt16))

        // target buffer of the string
        var utf16Array: [UInt16] = Array(repeating: 0, count: numberOfBytesToRead/sizeUInt16)

        utf16Array.withUnsafeMutableBufferPointer {bufPtr in
            countryBorderLineData.copyBytes(to: bufPtr, from: nextLocation...)
        }
        // advance the pointer
        nextLocation += numberOfBytesToRead

        // convert the read array into a string
        let resultString = String(utf16CodeUnits: utf16Array, count: utf16Array.count)
        print(resultString)
    }

    // read the number of countries
    _ = withUnsafeMutableBytes(of: &dataReadBufferUInt64) {bufPtr in
        countryBorderLineData.copyBytes(to: bufPtr, from: nextLocation...)
    }
    // advance the pointer
    nextLocation += sizeUInt64

    // This line SOMETIMES crashes (see crash subset of crash report)
    let numberOfCountries = Int(dataReadBufferUInt64)
    //...

}

我的猜测可能是错误的,上面的代码不能解决您的问题,在这种情况下,请显示有关您的数据的更多信息,我可以更正我的答案。

暂无
暂无

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

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