为什么在执行data.append(moredata)时迅速崩溃?或数据=数据1 +数据2

时间:2019-03-06 06:50:21

标签: swift xcode crash

我有一些快速代码,在处理从tcp套接字收集的Data()时会崩溃。从“几乎安静”的服务器上获取数据时,代码可以成功运行,但是在繁忙的服务器上,每次都会崩溃。

这是带有一些手动debugPrints的代码。我崩溃的行是“两个”和“三个”之间的“ data.append(incomingData)”。较早的版本在执行“数据= leftOverData +传入数据”时崩溃。

(class property)
var leftOverData: Data?

func analyze(incomingData: Data) {
    debugPrint("about to copy data")
    let size = incomingData.count + (leftOverData?.count ?? 0) + 10
    debugPrint("incoming data size \(incomingData.count) leftOverData.size \(String(describing: leftOverData?.count))")
    var data = Data(capacity: size)
    debugPrint("one")
    if leftOverData != nil {
        data = leftOverData!
        debugPrint("two")
        data.append(incomingData)
        debugPrint("three")
        self.leftOverData = nil
    } else {
        debugPrint("four")
        data = incomingData
    }

在我崩溃的那轮中,我有32个字节的leftOverData加上1448个字节的新传入数据。这是崩溃之前的手册:

"about to copy data"
"incoming data size 1448 leftOverData.size Optional(32)"
"one"
"two"
2019-03-05 22:28:35.487708-0800 Netrek[99969:6764765] [General] An uncaught exception was raised

开始编辑(更多数据):

添加了以下代码以在崩溃之前打印出数据内部:

    debugPrint("data startIndex \(data.startIndex) endIndex \(data.endIndex)\n")
    debugPrint("incomingData startIndex \(incomingData.startIndex) endIndex \(incomingData.endIndex)\n")
    debugPrint("size \(size)")
        data.append(incomingData)

获得了这些附加数据:

"data startIndex 14472 endIndex 14480\n"
"incomingData startIndex 0 endIndex 2896\n"
"size 2914"
2019-03-06 11:53:17.906992-0800 Netrek[24085:7434170] [General] An uncaught exception was raised
2019-03-06 11:53:17.907021-0800 Netrek[24085:7434170] [General] *** -[NSConcreteMutableData subdataWithRange:]: range {14472, 8} exceeds data length 13112

这是数据起源的代码(网络框架)。在接收关闭过程中,这似乎在第三轮崩溃。

    connection.receive(minimumIncompleteLength: 1, maximumLength: 32768) { (content, context, isComplete, error) in
        if let content = content {
            debugPrint("content startIndex \(content.startIndex) endIndex \(content.endIndex)" )
            self.delegate.gotData(data: content, from: self.hostname, port: self.port)
        }
    }

以下是接收关闭的索引信息:

"content startIndex 0 endIndex 1448"

我希望我知道数据长度13112来自哪里。

结束编辑

Xcode版本10.1(10B61)。迅捷4.2。

这是崩溃时我可以收集的所有数据:

Key message from below: [NSConcreteMutableData subdataWithRange:]: range {14448, 32} exceeds data length 11624'

Full stack trace: 

2019-03-05 22:28:35.487708-0800 Netrek[99969:6764765] [General] An uncaught exception was raised
2019-03-05 22:28:35.487742-0800 Netrek[99969:6764765] [General] *** -[NSConcreteMutableData subdataWithRange:]: range {14448, 32} exceeds data length 11624
2019-03-05 22:28:35.487847-0800 Netrek[99969:6764765] [General] (
    0   CoreFoundation                      0x00007fff31cf5f15 __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff5de61efb objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff31cf5d47 +[NSException raise:format:] + 193
    3   Foundation                          0x00007fff340870d9 -[NSData(NSData) subdataWithRange:] + 252
    4   libswiftFoundation.dylib            0x0000000100df2c76 $S10Foundation12_DataStorageC11mutableCopyyACSnySiGFTf4xn_n + 374
    5   libswiftFoundation.dylib            0x0000000100e121f9 $S10Foundation4DataV7_appendyySRyxGlFs5UInt8V_Tg5Tf4xn_n + 105
    6   libswiftFoundation.dylib            0x0000000100e12340 $S10Foundation4DataV6appendyyACFySRys5UInt8VG_SiSbztXEfU_TA + 16
    7   libswiftFoundation.dylib            0x0000000100d2e7d1 $S10Foundation12_DataStorageC14enumerateBytes2in_ySnySiG_ySRys5UInt8VG_SiSbztXEtFySV_So8_NSRangeVSpy10ObjectiveC8ObjCBoolVGtXEfU_ + 113
    8   libswiftFoundation.dylib            0x0000000100e0e7c1 $S10Foundation12_DataStorageC14enumerateBytes2in_ySnySiG_ySRys5UInt8VG_SiSbztXEtFySV_So8_NSRangeVSpy10ObjectiveC8ObjCBoolVGtXEfU_TA + 33
    9   libswiftFoundation.dylib            0x0000000100e59249 $S10Foundation12_DataStorageC14enumerateBytes2in_ySnySiG_ySRys5UInt8VG_SiSbztXEtFySV_So8_NSRangeVSpy10ObjectiveC8ObjCBoolVGtXEfU_TA.75 + 9
    10  libswiftFoundation.dylib            0x0000000100e0e701 $SSVSo8_NSRangeVSpy10ObjectiveC8ObjCBoolVGIgyyy_SVAbFIegyyy_TRTA + 17
    11  libswiftFoundation.dylib            0x0000000100e5c2b9 $SSVSo8_NSRangeVSpy10ObjectiveC8ObjCBoolVGIgyyy_SVAbFIegyyy_TRTA.79 + 9
    12  libswiftFoundation.dylib            0x0000000100d2bf0d $SSVSo8_NSRangeVSpy10ObjectiveC8ObjCBoolVGIegyyy_SVAbFIyByyy_TR + 29
    13  Foundation                          0x00007fff3402734f __49-[_NSDispatchData enumerateByteRangesUsingBlock:]_block_invoke + 37
    14  Foundation                          0x00007fff34027324 -[_NSDispatchData enumerateByteRangesUsingBlock:] + 62
    15  libswiftFoundation.dylib            0x0000000100d33d15 $S10Foundation4DataV6appendyyACF + 469
    16  Netrek                              0x0000000100005c63 $S6Netrek14PacketAnalyzerC7analyze12incomingDatay10Foundation0F0V_tF + 2483
    17  Netrek                              0x0000000100041614 $S6Netrek11AppDelegateC7gotData4data4from4porty10Foundation0E0V_SSSitF + 740
    18  Netrek                              0x00000001000416f0 $S6Netrek11AppDelegateCAA07NetworkC0A2aDP7gotData4data4from4porty10Foundation0F0V_SSSitFTW + 16
    19  Netrek                              0x00000001000352da $S6Netrek9TcpReaderC7receiveyyFy10Foundation4DataVSg_7Network12NWConnectionC14ContentContextCSgSbAI7NWErrorOSgtcfU_ + 2106
    20  Netrek                              0x0000000100035af7 $S6Netrek9TcpReaderC7receiveyyFy10Foundation4DataVSg_7Network12NWConnectionC14ContentContextCSgSbAI7NWErrorOSgtcfU_TA + 71
    21  libswiftNetwork.dylib               0x00000001010787a0 $S7Network12NWConnectionC14receiveMessage10completionyy10Foundation4DataVSg_AC14ContentContextCSgSbAA7NWErrorOSgtc_tFySo16OS_dispatch_dataCSg_So0K19_nw_content_context_pSgSbSo0k1_N6_error_pSgtcfU_TA + 304
    22  libswiftNetwork.dylib               0x0000000101083c49 $S7Network12NWConnectionC7receive23minimumIncompleteLength07maximumF010completionySi_Siy10Foundation4DataVSg_AC14ContentContextCSgSbAA7NWErrorOSgtctFySo16OS_dispatch_dataCSg_So0N19_nw_content_context_pSgSbSo0n1_Q6_error_pSgtcfU_TA + 9
    23  libswiftNetwork.dylib               0x0000000101068ba8 $SSo16OS_dispatch_dataCSgSo0A19_nw_content_context_pSgSbSo0a1_D6_error_pSgIegggyg_AcESbAGIeyByyyy_TR + 88
    24  libdispatch.dylib                   0x00000001012169eb _dispatch_block_async_invoke2 + 83
    25  libdispatch.dylib                   0x00000001012077c3 _dispatch_client_callout + 8
    26  libdispatch.dylib                   0x000000010120abcd _dispatch_continuation_pop + 576
    27  libdispatch.dylib                   0x0000000101209ca3 _dispatch_async_redirect_invoke + 780
    28  libdispatch.dylib                   0x000000010121a270 _dispatch_root_queue_drain + 334
    29  libdispatch.dylib                   0x000000010121ad33 _dispatch_worker_thread2 + 125
    30  libsystem_pthread.dylib             0x0000000101282119 _pthread_wqthread + 619
    31  libsystem_pthread.dylib             0x0000000101281e41 start_wqthread + 13
)
2019-03-05 22:28:35.493083-0800 Netrek[99969:6764765] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSConcreteMutableData subdataWithRange:]: range {14448, 32} exceeds data length 11624'

有什么想法我在做什么错吗?

结束编辑

Xcode版本10.1(10B61)。迅捷4.2。

这是崩溃时我可以收集的所有数据:

Key message from below: [NSConcreteMutableData subdataWithRange:]: range {14448, 32} exceeds data length 11624'

Full stack trace: 

2019-03-05 22:28:35.487708-0800 Netrek[99969:6764765] [General] An uncaught exception was raised
2019-03-05 22:28:35.487742-0800 Netrek[99969:6764765] [General] *** -[NSConcreteMutableData subdataWithRange:]: range {14448, 32} exceeds data length 11624
2019-03-05 22:28:35.487847-0800 Netrek[99969:6764765] [General] (
    0   CoreFoundation                      0x00007fff31cf5f15 __exceptionPreprocess + 256
    1   libobjc.A.dylib                     0x00007fff5de61efb objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff31cf5d47 +[NSException raise:format:] + 193
    3   Foundation                          0x00007fff340870d9 -[NSData(NSData) subdataWithRange:] + 252
    4   libswiftFoundation.dylib            0x0000000100df2c76 $S10Foundation12_DataStorageC11mutableCopyyACSnySiGFTf4xn_n + 374
    5   libswiftFoundation.dylib            0x0000000100e121f9 $S10Foundation4DataV7_appendyySRyxGlFs5UInt8V_Tg5Tf4xn_n + 105
    6   libswiftFoundation.dylib            0x0000000100e12340 $S10Foundation4DataV6appendyyACFySRys5UInt8VG_SiSbztXEfU_TA + 16
    7   libswiftFoundation.dylib            0x0000000100d2e7d1 $S10Foundation12_DataStorageC14enumerateBytes2in_ySnySiG_ySRys5UInt8VG_SiSbztXEtFySV_So8_NSRangeVSpy10ObjectiveC8ObjCBoolVGtXEfU_ + 113
    8   libswiftFoundation.dylib            0x0000000100e0e7c1 $S10Foundation12_DataStorageC14enumerateBytes2in_ySnySiG_ySRys5UInt8VG_SiSbztXEtFySV_So8_NSRangeVSpy10ObjectiveC8ObjCBoolVGtXEfU_TA + 33
    9   libswiftFoundation.dylib            0x0000000100e59249 $S10Foundation12_DataStorageC14enumerateBytes2in_ySnySiG_ySRys5UInt8VG_SiSbztXEtFySV_So8_NSRangeVSpy10ObjectiveC8ObjCBoolVGtXEfU_TA.75 + 9
    10  libswiftFoundation.dylib            0x0000000100e0e701 $SSVSo8_NSRangeVSpy10ObjectiveC8ObjCBoolVGIgyyy_SVAbFIegyyy_TRTA + 17
    11  libswiftFoundation.dylib            0x0000000100e5c2b9 $SSVSo8_NSRangeVSpy10ObjectiveC8ObjCBoolVGIgyyy_SVAbFIegyyy_TRTA.79 + 9
    12  libswiftFoundation.dylib            0x0000000100d2bf0d $SSVSo8_NSRangeVSpy10ObjectiveC8ObjCBoolVGIegyyy_SVAbFIyByyy_TR + 29
    13  Foundation                          0x00007fff3402734f __49-[_NSDispatchData enumerateByteRangesUsingBlock:]_block_invoke + 37
    14  Foundation                          0x00007fff34027324 -[_NSDispatchData enumerateByteRangesUsingBlock:] + 62
    15  libswiftFoundation.dylib            0x0000000100d33d15 $S10Foundation4DataV6appendyyACF + 469
    16  Netrek                              0x0000000100005c63 $S6Netrek14PacketAnalyzerC7analyze12incomingDatay10Foundation0F0V_tF + 2483
    17  Netrek                              0x0000000100041614 $S6Netrek11AppDelegateC7gotData4data4from4porty10Foundation0E0V_SSSitF + 740
    18  Netrek                              0x00000001000416f0 $S6Netrek11AppDelegateCAA07NetworkC0A2aDP7gotData4data4from4porty10Foundation0F0V_SSSitFTW + 16
    19  Netrek                              0x00000001000352da $S6Netrek9TcpReaderC7receiveyyFy10Foundation4DataVSg_7Network12NWConnectionC14ContentContextCSgSbAI7NWErrorOSgtcfU_ + 2106
    20  Netrek                              0x0000000100035af7 $S6Netrek9TcpReaderC7receiveyyFy10Foundation4DataVSg_7Network12NWConnectionC14ContentContextCSgSbAI7NWErrorOSgtcfU_TA + 71
    21  libswiftNetwork.dylib               0x00000001010787a0 $S7Network12NWConnectionC14receiveMessage10completionyy10Foundation4DataVSg_AC14ContentContextCSgSbAA7NWErrorOSgtc_tFySo16OS_dispatch_dataCSg_So0K19_nw_content_context_pSgSbSo0k1_N6_error_pSgtcfU_TA + 304
    22  libswiftNetwork.dylib               0x0000000101083c49 $S7Network12NWConnectionC7receive23minimumIncompleteLength07maximumF010completionySi_Siy10Foundation4DataVSg_AC14ContentContextCSgSbAA7NWErrorOSgtctFySo16OS_dispatch_dataCSg_So0N19_nw_content_context_pSgSbSo0n1_Q6_error_pSgtcfU_TA + 9
    23  libswiftNetwork.dylib               0x0000000101068ba8 $SSo16OS_dispatch_dataCSgSo0A19_nw_content_context_pSgSbSo0a1_D6_error_pSgIegggyg_AcESbAGIeyByyyy_TR + 88
    24  libdispatch.dylib                   0x00000001012169eb _dispatch_block_async_invoke2 + 83
    25  libdispatch.dylib                   0x00000001012077c3 _dispatch_client_callout + 8
    26  libdispatch.dylib                   0x000000010120abcd _dispatch_continuation_pop + 576
    27  libdispatch.dylib                   0x0000000101209ca3 _dispatch_async_redirect_invoke + 780
    28  libdispatch.dylib                   0x000000010121a270 _dispatch_root_queue_drain + 334
    29  libdispatch.dylib                   0x000000010121ad33 _dispatch_worker_thread2 + 125
    30  libsystem_pthread.dylib             0x0000000101282119 _pthread_wqthread + 619
    31  libsystem_pthread.dylib             0x0000000101281e41 start_wqthread + 13
)
2019-03-05 22:28:35.493083-0800 Netrek[99969:6764765] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSConcreteMutableData subdataWithRange:]: range {14448, 32} exceeds data length 11624'

有什么想法我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

我不确定为什么需要初始化容量为data的变量。您可以简单地定义var类型并在以后附加数据。

我尝试了以下操作,效果很好。

var leftOverData: Data?

// In viewDidLoad
leftOverData = "Hi my name is Ankit. ".data(using: .utf8)
self.analyze(incomingData: "What is your name in the context of lorem ipsum dolar sit ahem. This is an dummy text I am using to create Data.".data(using: .utf8)!)

// Updated the function as
func analyze(incomingData: Data) {
    var data = Data()
    if let existingData = self.leftOverData {
        data = existingData
        data.append(incomingData)
        self.leftOverData = nil
    } else {
        data = incomingData
    }
}