Re: Swift crash in dispatch_apply
Re: Swift crash in dispatch_apply
- Subject: Re: Swift crash in dispatch_apply
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Tue, 08 Jul 2014 09:45:38 +0700
On 8 Jul 2014, at 00:00, Clark S. Cox III <email@hidden> wrote:
>>
>> On Jul 4, 2014, at 23:05, Gerriet M. Denkmann <email@hidden> wrote:
>>
>> Why does this crash?
>>
>> class Crash : NSObject // must be subclass of NSObject to get a crash
>> {
>> var globalSize = 0
>> let queue = dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0 )
>>
>> func makeCrash()
>> {
>> globalSize = 97 // anything
>> let nbrThreads : UInt = 1 // any positive number will do; output might be garbled for > 1
>> let localSize = globalSize
>>
>> println("makeCrash will do dispatch_apply( \(nbrThreads) )")
>> dispatch_apply( nbrThreads, queue,
>> { [unowned self] (idx: size_t) in
>>
>> println( "thread[\(idx)] local \(localSize)") // ok
>>
>> // next line will crash --- Mac OS X 10.9.4 Xcode Version 6.0 (6A216f)
>> println( "thread[\(idx)] global \(self.globalSize)") // will crash
>> }
>> )
>> }
>> }
>>
>> let dummy = Crash()
>> dummy.makeCrash()
>
> What is the actual crash?
(lldb) bt
* thread #1: tid = 0x245c69, 0x00000001001b6273 libswift_stdlib_core.dylib`_swift_abortRetainUnowned + 19, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)
* frame #0: 0x00000001001b6273 libswift_stdlib_core.dylib`_swift_abortRetainUnowned + 19
frame #1: 0x00000001001c3a8f libswift_stdlib_core.dylib`swift_unknownRetainUnowned + 351
frame #2: 0x0000000100001e5d Prime CL`Prime_CL.Crash.makeCrash (self=0x0000123459616fe0)() -> () + 797 at HaSwiftAtom.swift:53
frame #3: 0x00000001000024a2 Prime CL`@objc Prime_CL.Crash.makeCrash (Prime_CL.Crash)() -> () + 34 at HaSwiftAtom.swift:0
[...]
> Why are you making self unowned?
I have no idea.
The Swift book (in the chapter "“Resolving Strong Reference Cycles for Closures”) seems to say that this is the correct way to "resolve a strong reference cycle between a closure and a class instance”.
But I don't know it such a strong reference cycle would exist in my case.
Indeed: removing " [unowned self] " also removes the crash.
Kind regards,
Gerriet.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden