EXC_BAD_ACCESS KERN_INVALID_ADDRESS 程序崩溃

9

我的应用在通过Xcode调试模式运行时都很好,但是当通过TestFlight下载后,会出现如下错误导致应用崩溃: EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x000200013f7b9e78

根据堆栈跟踪信息,我发现应用在这行代码处崩溃:specialized Array.withUnsafeMutableBufferPoint,很可能是与此相关的。

self.array.sortInPlace. {
                (($0 as Dictionary<String, AnyObject>)["time"] as? String) > (($1 as Dictionary<String, AnyObject>)["time"] as? String)
            }

在Swift 1.3中,我使用的是sort,现在在Swift 2中,我正在使用sortInPlace。不确定这是否有所不同,但我知道由于其是不可变的,sort的结果没有被调用。
Thread : Crashed: com.apple.main-thread
    0  libswiftCore.dylib             4302688056 _swift_retain_(swift::HeapObject*) + 8
    1  App                          4295962040 specialized LiveFeedViewController.(viewDidLoad(LiveFeedViewController) -> () -> ()).(closure #3).(closure #1) (LiveFeedViewController.swift)
    2  App                          4295785356 specialized thunk (LiveFeedViewController.swift)
    3  App                          4295953880 specialized _siftDown<A where ...> (inout A, A.Index, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool) -> () (LiveFeedViewController.swift)
    4  App                          4295952448 specialized _heapSort<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool) -> () (LiveFeedViewController.swift)
    5  App                          4295938144 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    6  App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    7  App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    8  App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    9  App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    10 App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    11 App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    12 App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    13 App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    14 App                          4295938120 specialized _introSortImpl<A where ...> (inout A, Range<A.Index>, inout (A.Generator.Element, A.Generator.Element) -> Bool, Int) -> () (LiveFeedViewController.swift)
    15 App                          4295960640 specialized specialized MutableCollectionType<A where ...>.sortInPlace<A where ...>((A.Generator.Element, A.Generator.Element) -> Bool) -> () (LiveFeedViewController.swift)
    16 App                          4295730388 _TPA__TTSg5GSaGVSs10DictionarySSPSs9AnyObject___GSaGS_SSPS0____Ss21MutableCollectionTypeSs_GVSs17IndexingGeneratorGSaGS_SSPS0_____GS2_GSaGS_SSPS0_____Ss13GeneratorTypeSs_GS_SSPS0____SiSiSs21RandomAccessIndexTypeSs_SiSiSs18_SignedIntegerTypeSs_SiSiSs33_BuiltinIntegerLiteralConvertibleSs_SiSiSs16SignedNumberTypeSs_SiSiS6_Ss_Si_GVSs10ArraySliceGS_SSPS0____GS8_GS_SSPS0____Ss9IndexableSsGS8_GS_SSPS0____Ss12SequenceTypeSs_GS2_GS8_GS_SSPS0_____GS2_GS8_GS_SSPS0_____S3_Ss_GS_SSPS0____SiSiSs16ForwardIndexTypeSs_SiSiS5_Ss_SiSiS6_Ss_Si_GS8_GS_SSPS0_____GS_SSPS0____GS_SSPS0______TFFeRq_Ss21MutableCollectionTypeqq_Ss16MutableIndexable5IndexSs21RandomAccessIndexType_SsS_11sortInPlaceuRq_S_qq_S0_5IndexS1__FRq_FFTqqq_Ss14CollectionType9GeneratorSs13GeneratorType7Elementqqq_S2_9GeneratorS3_7Element_SbT_U_FTGVSs20UnsafeMutablePointerQQQPS_9Generator7Element_Si_T_ (LiveFeedViewController.swift)
    17 App                          4295730548 _TPA__TTSg5GSaGVSs10DictionarySSPSs9AnyObject___GSaGS_SSPS0____Ss21MutableCollectionTypeSs_GVSs17IndexingGeneratorGSaGS_SSPS0_____GS2_GSaGS_SSPS0_____Ss13GeneratorTypeSs_GS_SSPS0____SiSiSs21RandomAccessIndexTypeSs_SiSiSs18_SignedIntegerTypeSs_SiSiSs33_BuiltinIntegerLiteralConvertibleSs_SiSiSs16SignedNumberTypeSs_SiSiS6_Ss_Si_GVSs10ArraySliceGS_SSPS0____GS8_GS_SSPS0____Ss9IndexableSsGS8_GS_SSPS0____Ss12SequenceTypeSs_GS2_GS8_GS_SSPS0_____GS2_GS8_GS_SSPS0_____S3_Ss_GS_SSPS0____SiSiSs16ForwardIndexTypeSs_SiSiS5_Ss_SiSiS6_Ss_Si_GS8_GS_SSPS0_____GS_SSPS0____GS_SSPS0______TTRGRq_Ss21MutableCollectionTypeqq_Ss16MutableIndexable5IndexSs21RandomAccessIndexType_XFo_dGVSs20UnsafeMutablePointerqqq_Ss14CollectionType9GeneratorSs13GeneratorType7Element_dSi_dT_zoPSs9ErrorType__XFo_dGS2_qqq_S3_9GeneratorS4_7Element_dSi_iT_zoPS5___ (LiveFeedViewController.swift)
    18 App                          4295730716 _TPA__TTSg5GVSs10DictionarySSPSs9AnyObject___T____TFFSa42_withUnsafeMutableBufferPointerIfSupportedu__rFRGSaq__FzFzTGVSs20UnsafeMutablePointerq__Si_qd__GSqqd___U_FzRGVSs26UnsafeMutableBufferPointerQd___Q_ (LiveFeedViewController.swift)
    19 App                          4295954668 specialized Array.withUnsafeMutableBufferPointer<A><B>((inout UnsafeMutableBufferPointer<A>) throws -> B) throws -> B (LiveFeedViewController.swift)
    20 App                          4295959880 specialized specialized MutableCollectionType<A where ...>.sortInPlace<A where ...>((A.Generator.Element, A.Generator.Element) -> Bool) -> () (LiveFeedViewController.swift)
    21 App                          4295895596 LiveFeedViewController.(viewDidLoad(LiveFeedViewController) -> () -> ()).(closure #3) (LiveFeedViewController.swift:1862)
    22 libdispatch.dylib              6875551464 _dispatch_call_block_and_release + 24
    23 libdispatch.dylib              6875551400 _dispatch_client_callout + 16
    24 libdispatch.dylib              6875573680 _dispatch_main_queue_callback_4CF + 1844
    25 CoreFoundation                 6518931960 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
    26 CoreFoundation                 6518923360 __CFRunLoopRun + 1628
    27 CoreFoundation                 6518066336 CFRunLoopRunSpecific + 384
    28 GraphicsServices               6704955528 GSEventRunModal + 180
    29 UIKit                          6609391612 UIApplicationMain + 204
    30 App                          4296344512 main (AppDelegate.swift:23)
    31 libdyld.dylib                  6875752632 start + 4

更新:

以下内容均以Swift 2为基础,该数组是由一系列字典组成的数组,声明为var array = [[String:AnyObject]]()。通过追加一系列字典来填充它,例如:

self.dict["foo"] = 0
self.dict["bar"] = "dog"
self.array.append(self.dict)

我的担忧是这段代码位于dispatch_group_notify中,它正在等待一系列dispatch_group_leave。这可能与问题有关。代码如下:

dispatch_group_notify(group, queue) { () -> Void in

      self.array.sortInPlace {
          (($0["foo"] as? Int) > (($1 )["foo"] as? Int))
      }
}

那么这个数组是什么,如何声明它,如何填充它,里面有什么? - matt
@matt 看到更新了吗?如果需要更多信息,请告诉我。 - slider
这可能与问题无关,但我很难理解尝试对_Optionals_进行排序的意义所在。如果您知道这些内容是Int或String或其他类型,那么应该按照Int或String或其他类型进行排序。如果转换时存在失败的可能性,那么您期望发生什么情况? - matt
我无法将二进制运算符应用于两个 AnyObject,因此我需要将数组的字典值转换为某些类型,这里是 Int。没有失败的机会。我只是解包了它们,感谢您的提醒,但我不确定那是否是我的主要问题。 - slider
强制类型转换没问题,但你是在进行可选类型的强制转换。($0["foo"] as? Int) 不是 Int 类型,而是一个可选类型。虽然这可能与本情况无关,但在这种情况下这样做是非常奇怪的。 - matt
显示剩余3条评论
4个回答

7

这是一个老问题,但是在调试运行中难以重现EXC_BAD_ACCESS KERN_INVALID_ADDRESS的主要原因是释放对象的生命周期在调试版本和发布版本中不同。 在后者中,内存立即消失并可能被替换为与其无关的对象,而在前者中,内存可能仍被短暂地保留,因此您的应用程序仍然可以正常运行。 此外,当这种情况在发布版本中发生时,崩溃跟踪的最后一行可能会令人困惑,因为所引用的对象可能是完全不相关的类型。 如果是这种情况,我们应该在堆栈树中向上查找一点,并希望在那里找到更多线索。


6
我遇到了同样的问题,但只影响约50%的设备,仅限于运行应用程序发布版本的设备。我的代码没有使用类似dispatch_group的任何内容。
在我的情况下,我正在使用以下代码:
contacts.sortInPlace({ $0.name.localizedCaseInsensitiveCompare($1.name) == .OrderedAscending })

当我用以下内容替换它时,它被修复了:

contacts = contacts.sort({ $0.name.localizedCaseInsensitiveCompare($1.name) == .OrderedAscending })

由于它只影响少量用户,在低级别的Swift API中崩溃,并且通过替换为sort来修复,我认为这是苹果代码中的错误。

我建议向苹果报告错误并在此期间使用sort

注意:你的代码和我的代码之间的另一个相似之处是使用简写参数名称(即$0和$1)。但是,即使我用命名参数替换它们,它仍然会崩溃。

更新:许多人在苹果论坛上遇到了同样的问题。根据该帖子,最新的Xcode beta版已经解决了这个问题。


3

关于UPDATE中的代码:

由于数组是“手动”填充的,最可靠(有效并符合Swift类型安全性)的方法是使用自定义类和非可选属性,而不是通用字典。

class Item {
  var foo : Int
  var bar : String

  init(foo : Int, bar : String) {
    self.foo = foo
    self.bar = bar
  }
}

var array = [Item]()
array.append(Item(foo: 0, bar: "dog"))
array.append(Item(foo: 1, bar: "cat"))

array.sortInPlace { $0.foo > $1.foo }

所有类型都是众所周知的且不可选。

无需类型转换、无需解包、无需额外开销,没有问题。


0

我遇到这个错误的一个情况是,在Objective-C中声明了一个nonnull属性,但忘记初始化它,然后在Swift中尝试使用它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接