vaid
vaid

Reputation: 1550

Unknown error when HTTP POSTing in Swift 3 iOS

I'm working on an app that sends some data from an asset to a webserver using the JustHTTP framework for Swift 3.

I initially sent data using my own code.

When data has been sent and a response is received from the webserver, the app sometimes completely randomly crashes with an error.

Sometimes this occurs, sometimes it doesnt.

I've tracked down the problem to the URLSessions part of the code and the error ends with a stacktrace.

But how do I fix this error? What's the reason this happens?

The error is:

and the trace looks like this:

com.apple.NSURLConnectionLoader (9): EXEC_BAD_ACCESS (code=1, address=0.33)

libBacktraceRecording.dylib`__gcd_queue_item_enqueue_hook_block_invoke:

    0x10e43dea8 <+0>:  pushq  %rbp
    0x10e43dea9 <+1>:  movq   %rsp, %rbp
->  0x10e43deac <+4>:  movq   0x20(%rsi), %rax
    0x10e43deb0 <+8>:  movq   0x20(%rdi), %rcx
    0x10e43deb4 <+12>: cmpq   0x8(%rcx), %rax
    0x10e43deb8 <+16>: sete   %al
    0x10e43debb <+19>: popq   %rbp
    0x10e43debc <+20>: retq   

EDIT I suspect it all starts here:

func sendChunk(chunkOwner: extractionResults, chunkData: Data, chunkIndex: Int, onSuccess: @escaping (assetInfo) -> Void){


        let params = [
            "id"            : chunkOwner.assetInfo.id                        ,
            "filename"      : chunkOwner.assetInfo.filename                  ,
            "part_number"   : String(chunkIndex)                             ,
            "size"          : String(describing: chunkOwner.assetInfo.size  ),
            "md5"           : chunkOwner.MD5                                 ,
        ]



        let partName: String = params["filename"]! + "." + params["id"]! + ".part" +  params["part_number"]!

        if let response = Just.post( "http://192.168.0.23/upload.php", data:params, files:["file":.data(partName, chunkData, nil)]).content {



            let fn = JSON(data: response)["Filename"].stringValue
            let ci = JSON(data: response)["Part_Number"].intValue

            let msg1: String = fn + " (" + String(chunkData.count) + "/"
            let msg2: String = String(chunkOwner.assetInfo.size) + " bytes) "
            let msg3: String = "chunk " + String(ci+1) + " of "
            let msg4: String = String(chunkOwner.assetInfo.chunkCount()) + " was successfully uploaded"
            let msg = msg1 + msg2 + msg3 + msg4

            print( msg)

            onSuccess(chunkOwner.assetInfo)
        }
}

NOTE:

The object extractionResults is part of the AssetManager framework found here for reference: https://github.com/aidv/AssetManager

EDIT 2 Just now I received the error

Thread 11: EXC_BAD_ACCESS (code=EXC_i386_GPFLT)

Same trace as before.

EDIT 3

Seems as if the problem appears when POSTs occur too fast one-after-the-other.

If I make POSTs with at least 0.5 seconds pauses then the problem seems to disappear.

I have read on other places that similar errors occur when an URL task is resumed and cancelled to quickly.

Upvotes: 1

Views: 335

Answers (2)

dgatwood
dgatwood

Reputation: 10417

The libBacktraceRecording.dylib library is part of Xcode's debug support. You should never see this particular crash in a non-debug build.

If you need to do any workarounds (e.g. slowing down the rate of requests), be sure to wrap them in #if DEBUG.

Also, be sure you aren't repeatedly creating and tearing down sessions.

Beyond that, you can always turn off backtrace recordings in your scheme. See also: Memory leak with "libBacktraceRecording.dylib" in React Native ios application

Upvotes: 2

vaid
vaid

Reputation: 1550

Temporary fix (will never be accepted as the correct answer, because it's not):

Use a Timer object to delay the next POST call with at least 400 milliseconds.

Upvotes: 0

Related Questions