Swift: another crash
Swift: another crash
- Subject: Swift: another crash
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Tue, 08 Jul 2014 17:10:58 +0700
Why does this( sometimes) crash?
func findAndCountFrom( staP : UnsafePointer<UInt8>, to endP : UnsafePointer<UInt8> ) -> UInt
{
let functionName = __FUNCTION__
var nbrOfThreads : UInt = 4
var theCounts = [UInt](count: Int(nbrOfThreads), repeatedValue: 0)
println( functionName + " dummy count using \(nbrOfThreads) threads")
dispatch_apply( nbrOfThreads, queue,
{ (idx: size_t) in
var localCount : UInt = idx
println("will do: theCounts[\(idx)] = \(localCount)")
theCounts[ Int(idx) ] = localCount;
}
)
var countA : UInt = 0
for i in 0 ..< nbrOfThreads { countA += theCounts[ Int(i) ] }
println( functionName + " sum of \(nbrOfThreads) counts = \(countA)")
return countA;
}
It sometime prints:
findAndCountFrom(_:to:) dummy count using 4 threads
will do: theCounts[0] = 0
will do: theCounts[3] = 3
will do: theCounts[1] = 1
will do: theCounts[2] = 2
findAndCountFrom(_:to:) sum of 4 counts = 6
Some other times it prints:
findAndCountFrom(_:to:) dummy count using 4 threads
will do: theCounts[1] = 1
will do: theCounts[3] = 3
wwiillll ddoo:: tthheeCCoouunnttss[[02]] == 02
findAndCountFrom(_:to:) sum of 4 counts = 6
Which raises the question: is there a print function which is more thread resilient than println() ?
But - and this is the real problem - sooner or later I get:
findAndCountFrom(_:to:) dummy count using 4 threads
will do: theCounts[0] = 0
wi l l dw oi :lw lit lhdleo C:do out:nh tetsCh[oe3uC]no tu=sn [t31s
][ 2=] 1=
2
(lldb) bt
* thread #19: tid = 0x26ed11, 0x00000001001bbd6e libswift_stdlib_core.dylib`_swift_release_(swift::HeapObject*) + 14, queue = 'com.apple.root.high-priority', stop reason = EXC_BAD_ACCESS (code=1, address=0x61801916dfc8)
frame #0: 0x00000001001bbd6e libswift_stdlib_core.dylib`_swift_release_(swift::HeapObject*) + 14
frame #1: 0x00000001000c3c40 libswift_stdlib_core.dylib`Swift.Array.makeUnique <A>(Swift.Array<A>)(@inout Swift.ArrayBuffer<A>, e : A, index : Swift.Int) -> () + 448
frame #2: 0x00000001000c3a1b libswift_stdlib_core.dylib`Swift.Array.subscript.setter (Swift.Int) -> A + 283
* frame #3: 0x0000000100016118 Prime CL`Prime_CL.HaSwiftAtom.(idx=Darwin.size_t at 0x000061800f97ec90, theCounts=Swift.Array<Swift.UInt> at 0x000061800f97ecf8) -> (Swift.UnsafePointer<Swift.UInt8>, to : Swift.UnsafePointer<Swift.UInt8>) -> Swift.UInt).(closure #1) + 840 at HaSwiftAtom.swift:1303
frame #4: 0x0000000100015547 Prime CL`reabstraction thunk helper from @callee_owned (@unowned Swift.UInt) -> (@unowned ()) to @callee_owned (@in Swift.UInt) -> (@out ()) + 23 at HaSwiftAtom.swift:712
frame #5: 0x0000000100012f81 Prime CL`partial apply forwarder for reabstraction thunk helper from @callee_owned (@unowned Swift.UInt) -> (@unowned ()) to @callee_owned (@in Swift.UInt) -> (@out ()) with unmangled suffix "61" + 81 at HaSwiftAtom.swift:0
frame #6: 0x0000000100015580 Prime CL`reabstraction thunk helper from @callee_owned (@in Swift.UInt) -> (@out ()) to @callee_owned (@unowned Swift.UInt) -> (@unowned ()) + 32 at HaSwiftAtom.swift:712
frame #7: 0x00000001000155bf Prime CL`reabstraction thunk helper from @callee_owned (@unowned Swift.UInt) -> (@unowned ()) to @callee_unowned @objc_block (@unowned Swift.UInt) -> (@unowned ()) + 47 at HaSwiftAtom.swift:712
frame #8: 0x00000001003b8a74 libdispatch.dylib`_dispatch_client_callout2 + 8
frame #9: 0x00000001003b8949 libdispatch.dylib`_dispatch_apply_invoke + 138
frame #10: 0x00000001003af925 libdispatch.dylib`_dispatch_client_callout + 8
frame #11: 0x00000001003b1c3d libdispatch.dylib`_dispatch_root_queue_drain + 601
frame #12: 0x00000001003b32e6 libdispatch.dylib`_dispatch_worker_thread2 + 52
frame #13: 0x00007fff8dfcaef8 libsystem_pthread.dylib`_pthread_wqthread + 314
frame #14: 0x00007fff8dfcdfb9 libsystem_pthread.dylib`start_wqthread + 13
(lldb)
What am I doing wrong?
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