3

I have following code snippets where sometimes this line: self.onPostExecute(transItem) leads to application crash:

func execute(){

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
            //Your code to execute in background...

             println("Your code to execute in background...")
            var  transItem:WmTransferItem = self.doInBackground()

            dispatch_async(dispatch_get_main_queue(), {
                 println("code to be executed on the main thread when background task is finished")
                self.onPostExecute(transItem) // line 639
                });
            });            
    }

What does that mean: with unmangled suffix "_promote0"

Exception:

Thread : Crashed: com.apple.main-thread
0  MyApplication                       0x000e9ed8 MyApplication.WmBuildGroupsTask.onPostExecute (MyApplication.WmBuildGroupsTask)(MyApplication.WmTransferItem) -> () (WmBuildGroupsTask.swift:419)
1  libswiftCore.dylib             0x0045803b swift_reportFatalError + 162
2  MyApplication                       0x000f2ddc MyApplication.WmBuildGroupsTask.(execute (MyApplication.WmBuildGroupsTask) -> () -> ()).(closure #1).(closure #1) with unmangled suffix "_promote0" (WmBuildGroupsTask.swift:639)
3  MyApplication                       0x000f2e34 reabstraction thunk helper from @callee_owned () -> (@unowned ()) to @callee_unowned @objc_block () -> (@unowned ()) (WmBuildGroupsTask.swift)
4  libdispatch.dylib              0x3a133d53 _dispatch_call_block_and_release + 10
5  libdispatch.dylib              0x3a133d3f _dispatch_client_callout + 22
6  libdispatch.dylib              0x3a1366c3 _dispatch_main_queue_callback_4CF + 278
7  CoreFoundation                 0x2f47a641 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
8  CoreFoundation                 0x2f478f0d __CFRunLoopRun + 1308
9  CoreFoundation                 0x2f3e3729 CFRunLoopRunSpecific + 524
10 CoreFoundation                 0x2f3e350b CFRunLoopRunInMode + 106
11 GraphicsServices               0x343526d3 GSEventRunModal + 138
12 UIKit                          0x31d44871 UIApplicationMain + 1136
13 MyApplication                       0x00166417 main (main.m:32)

What I'm doing wrong?

Thanks,

snaggs
  • 5,543
  • 17
  • 64
  • 127
  • Did you ever figure out what was the issue here? – Andy Tsen Oct 22 '14 at 05:25
  • @AndyTsen, I have no clue. It doesn't happens consistently. – snaggs Oct 22 '14 at 11:32
  • I had the same issue. But it was reproduced only for release configuration for me. This topic helped me http://stackoverflow.com/q/25629841 Please look onto Matt Gibson's comment If you turn off Swift compiler optimisations for the release build, and try it again, do you still get the crash? (In build settings, under Swift Compiler/Code Generation) – Matt Gibson Sep 2 at 19:13 – Antony Olimenko Nov 24 '14 at 08:16

1 Answers1

0

I can't accept this answer as write answer but for now this example works properly and I don't have above mentioned crash in crash reports.

Here we go:

I use custom queue based on bundle name instead dispatch_get_global_queue:

let queue = dispatch_queue_create("<BUNDLE_NAME>", nil)

Example

func execute(){

    let queue = dispatch_queue_create("<BUNDLE_NAME>", nil)

    dispatch_async(queue, {
        //Your code to execute in background...

         println("Your code to execute in background...")
        var  transItem:WmTransferItem = self.doInBackground()

        dispatch_async(dispatch_get_main_queue(), {
             println("code to be executed on the main thread when background task is finished")
            self.onPostExecute(transItem)
            });
        });

}

Hope it will help to someone.

snaggs
  • 5,543
  • 17
  • 64
  • 127
  • 1
    By passing `nil` into `dispatch_queue_create`, you are creating a **serial queue**; in your original code, the queue you got from `dispatch_get_global_queue` is a **concurrent queue**. That might have something to do with the crash your saw. – Siyu Song Jan 24 '15 at 20:41