EXC_BAD_ACCESS when converting Swift string to data

The application crashes when calling this function in the Release assembly (for Debug, it works flawlessly)

func crashMe()
{
    func crashHelper(str: String) {}

    var crashString = "123"
    crashString.remove(at: crashString.startIndex)

    crashHelper(str: "\(crashString)")

    crashString.data(using: .ascii)
}

Almost always plays on a real device and often on a simulator (but not 100%) with iOS 9 or 10

Removing any row from this function prevents a crash

What am I doing wrong or maybe it's a Swift bug?

It can crash in different places, one of the crash logs:

0x02194b8a in swift_unknownRelease ()

0x020702c5 in _NSContiguousString .__ deallocating_deinit ()

0x024cea26 in String.data (using: String.Encoding, allowLossyConversion: Bool) -> Data? ()

0x0007e04f in the specialized application AppDelegate.crashMe () → ()

0x0007c31c in AppDelegate.crashMe () → () [inlined] ()

+6
1

, , .

macOS Release. main.swift.

import Foundation

func crashMe() {

    func crashHelper(str: String) {
//        print("crashHelper() got \(str)")  // line 1
        print("In crashHelper()!")  // line 2
//        var i = 123; i += 321; print("In crashHelper() the int is \(i)")  // line 3
    }

    var crashString = "123"
    crashString.remove(at: crashString.startIndex)
    crashHelper(str: "\(crashString)")

//    if let stringData = crashString.data(using: .ascii) {
//        print("stringData has \(stringData.count) bytes")
//    }
}

crashMe()
print("Success!!!")

:

  • < <23 > swift_unknownRetain.
  • 2 crashHelper() , , if , , crashString.data().
  • 1 / 3 , 2.
  • crashHelper() ; , .
  • crashString crashHelper() .

, , Swift , .

+1

Source: https://habr.com/ru/post/1015422/


All Articles