ios - 从 NSDataAsset 读取时偶尔崩溃

标签 ios swift crash nsdata memory-alignment

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

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

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

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

欢迎任何帮助/评论!

数据结构:

Assets 是以编程方式生成的文件。代码片段正在处理此文件的 header 。

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

字符串有两个元素: 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)
    //...

}

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

关于ios - 从 NSDataAsset 读取时偶尔崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57715807/

相关文章:

ios - 如何在iOS中正确处理 View Controller 中的数据持久性和状态警报

ios - 如何从订阅的 RACSignal 中获取错误值?

c++ - 单例的段错误

php - 我应该如何让我的 PHP API 为 iPhone 应用程序工作

ios - 如何从 ios 主屏幕上的书签图标返回到应用程序?

swift - 检查选择器中的数据 swift

crash - 是什么导致我的应用崩溃而没有错误

internet-explorer - 我的电脑上的 Internet Explorer 网站经常崩溃

ios - Facebook iOS 移动 Web JS SDK 错误

swift - 无法从嵌套的 json swift 获取数据