Crash (SIGABRT) when writing data to UserDefaults after Sheet disappears - ios

I got three similar crash reports that I can't reproduce (all on iOS 14.4). The stracktrace says the following (I only pasted the part where my app is starting):
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Triggered by Thread: 0
Thread 0 name:
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x00000001c077d414 __pthread_kill + 8
1 libsystem_pthread.dylib 0x00000001de2d8b50 pthread_kill + 272 (pthread.c:1392)
2 libsystem_c.dylib 0x000000019bc5bb74 abort + 104 (abort.c:110)
3 libswiftCore.dylib 0x0000000196795f20 swift::fatalError(unsigned int, char const*, ...) + 60 (Errors.cpp:393)
4 libswiftCore.dylib 0x0000000196796078 swift::swift_abortRetainUnowned(void const*) + 36 (Errors.cpp:460)
5 libswiftCore.dylib 0x00000001967e5844 swift_unknownObjectUnownedLoadStrong + 76 (SwiftObject.mm:895)
6 SwiftUI 0x00000001992b0cdc ViewGraph.graphDelegate.getter + 16 (ViewGraph.swift:234)
7 SwiftUI 0x00000001997e4d58 closure #1 in GraphHost.init(data:) + 80
8 SwiftUI 0x00000001997e6550 partial apply for closure #1 in GraphHost.init(data:) + 40 (<compiler-generated>:0)
9 AttributeGraph 0x00000001bbcc9b88 AG::Graph::Context::call_update() + 76 (ag-closure.h:108)
10 AttributeGraph 0x00000001bbcca1a0 AG::Graph::call_update() + 56 (ag-graph.cc:176)
11 AttributeGraph 0x00000001bbccfd70 AG::Subgraph::update(unsigned int) + 92 (ag-graph.h:709)
12 SwiftUI 0x00000001997e1cdc GraphHost.runTransaction() + 172 (GraphHost.swift:491)
13 SwiftUI 0x00000001997e4e1c GraphHost.runTransaction(_:) + 92 (GraphHost.swift:471)
14 SwiftUI 0x00000001997e37a8 GraphHost.flushTransactions() + 176 (GraphHost.swift:459)
15 SwiftUI 0x00000001997e2c78 specialized GraphHost.asyncTransaction<A>(_:mutation:style:) + 252 (<compiler-generated>:0)
16 SwiftUI 0x00000001993bd2fc AttributeInvalidatingSubscriber.invalidateAttribute() + 236 (AttributeInvalidatingSubscriber.swift:89)
17 SwiftUI 0x00000001993bd1f8 AttributeInvalidatingSubscriber.receive(_:) + 100 (AttributeInvalidatingSubscriber.swift:53)
18 SwiftUI 0x00000001993bd914 protocol witness for Subscriber.receive(_:) in conformance AttributeInvalidatingSubscriber<A> + 24 (<compiler-generated>:0)
19 SwiftUI 0x000000019956ba34 SubscriptionLifetime.Connection.receive(_:) + 100 (SubscriptionLifetime.swift:195)
20 Combine 0x00000001a6e67900 ObservableObjectPublisher.Inner.send() + 136 (ObservableObject.swift:115)
21 Combine 0x00000001a6e670a8 ObservableObjectPublisher.send() + 632 (ObservableObject.swift:153)
22 Combine 0x00000001a6e4ffdc PublishedSubject.send(_:) + 136 (PublishedSubject.swift:82)
23 Combine 0x00000001a6e76994 specialized static Published.subscript.setter + 388 (Published.swift:0)
24 Combine 0x00000001a6e75f74 static Published.subscript.setter + 40 (<compiler-generated>:0)
25 MyApp 0x00000001005d1228 counter.set + 32 (Preferences.swift:0)
26 MyApp 0x00000001005d1228 Preferences.counter.modify + 120 (Preferences.swift:0)
27 MyApp 0x00000001005ca440 MyView.changeCounter(decrease:) + 344 (MyView.swift:367)
28 MyApp 0x00000001005cf110 0x100584000 + 307472
29 MyApp 0x00000001005e65d8 thunk for #escaping #callee_guaranteed () -> () + 20 (<compiler-generated>:0)
30 MyApp 0x00000001005a8828 closure #2 in MySheet.body.getter + 140 (MySheet.swift:0)
What is happening is, that I have a Sheet with a button and when clicking on it the sheet disappears and in the onDisappear the changeCounter method in the main View MyView is called to change the counter. The method changeCounter is passed to the Sheet from MyView when calling/opening the Sheet.
This is the .sheet method in MyView:
.sheet(item: $activeSheet) { item in
switch item {
case .MY_SHEET:
MySheet(changeCounter: {changeCounter(decrease: true)}, changeTimer, item: $activeSheet)
}
}
This is the (important part of the) sheet:
struct MySheet: View {
var changeCounter: () -> Void
var changeTimer: () -> Void
#Binding var item: ActiveSheet?
#State var dismissAction: (() -> Void)?
var body: some View {
GeometryReader { metrics in
VStack {
Button(action: {
self.dismissAction = changeCounter
self.item = nil
}, label: {
Text("change_counter")
})
Button(action: {
self.dismissAction = changeTimer
self.item = nil
}, label: {
Text("change_timer")
})
}.frame(width: metrics.size.width, height: metrics.size.height * 0.85)
}.onDisappear(perform: {
if self.dismissAction != nil {
self.dismissAction!()
}
})
}
}
Here is changeCounter with the preferences object:
struct MyView: View {
#EnvironmentObject var preferences: Preferences
var body: some View {...}
func changeCounter(decrease: Bool) {
if decrease {
preferences.counter -= COUNTER_INTERVAL
}
}
}
The Preferences is an ObservableObject with the counter variable:
class Preferences: ObservableObject {
let userDefaults: UserDefaults
init(_ userDefaults: UserDefaults) {
self.userDefaults = userDefaults
self.counter = 0
}
#Published var counter: Int {
didSet {
self.userDefaults.set(counter, forKey: "counter")
}
}
}
It changes a value in the userDefaults that are UserDefaults.standard.
Anyone has an idea how that crash can happen and in what situations? Because it only happened three times now on users devices and I can't reproduce it.

Let's analyze
Button(action: {
self.dismissAction = changeCounter 1)
self.item = nil 2)
}, label: {
Line 1) changes internal sheet state initiating update of sheet's view
Line 2) changes external state initiating close of sheet (and probably update of parent view).
It even sounds as two conflicting process (even if there are no dependent flows, but looking at your code second depends on result of first). So, this is very dangerous logic and should be avoided.
In general, as I wrote in comment, changing two states in one closure is always risky, so I would rewrite logic to have something like (sketch):
Button(action: {
self.result = changeCounter // one external binding !!
}, label: {
, ie. the one state change that initiates some external activity...
Possible workaround for your code (if for any reason you cannot change logic) is to separate changes of those states in time, like
Button(action: {
self.dismissAction = changeCounter // updates sheet
DispatchQueue.main.async { // or after some min delay
self.item = nil // closes sheet after (!) update
}
}, label: {

Related

Combine's receive(on:) not dispatching to serial queue, causing data race

According to Apple, receive(on:options:) runs callbacks on a given queue. We use a serial dispatch queue to prevent racing on localOptionalCancellable in the code below. But receiveCancel is not getting dispatched to that queue. Can someone tell me why?
From the documentation,
You use the receive(on:options:) operator to receive results and completion on a specific scheduler, such as performing UI work on the main run loop.
...
Prefer receive(on:options:) over explicit use of dispatch queues when performing work in subscribers. For example, instead of the following pattern:
Issue Reproduction:
import Foundation
import Combine
class Example {
private var localOptionalCancellable: AnyCancellable?
private let dispatchQueue = DispatchQueue(label: "LocalQueue-\(UUID())")
func misbehavingFunction() {
self.dispatchQueue.async {
self.localOptionalCancellable = Just(())
.setFailureType(to: Error.self)
.receive(on: self.dispatchQueue)
.handleEvents(
receiveCancel: {
// Simultaneous accesses to 0x600000364e10, but modification requires exclusive access.
// Can be fixed by wrapping in self.dispatchQueue.async {}
self.localOptionalCancellable = nil
}
)
.sink(
receiveCompletion: { _ in },
receiveValue: { _ in
self.localOptionalCancellable = nil
}
)
}
}
}
Example().misbehavingFunction()
Stack Trace:
Simultaneous accesses to 0x600000364e10, but modification requires exclusive access.
Previous access (a modification) started at (0x10eeaf12a).
Current access (a modification) started at:
0 libswiftCore.dylib 0x00007fff2ff7be50 swift_beginAccess + 568
3 Combine 0x00007fff4ba73a40 Publishers.HandleEvents.Inner.cancel() + 71
4 Combine 0x00007fff4ba74230 protocol witness for Cancellable.cancel() in conformance Publishers.HandleEvents<A>.Inner<A1> + 16
5 Combine 0x00007fff4b9f10c0 Subscribers.Sink.cancel() + 652
6 Combine 0x00007fff4b9f1500 protocol witness for Cancellable.cancel() in conformance Subscribers.Sink<A, B> + 16
7 Combine 0x00007fff4b9dd2d0 AnyCancellable.cancel() + 339
8 Combine 0x00007fff4b9dd5f0 AnyCancellable.__deallocating_deinit + 9
9 libswiftCore.dylib 0x00007fff2ff7da20 _swift_release_dealloc + 16
13 Combine 0x00007fff4b9f0da0 Subscribers.Sink.receive(_:) + 54
14 Combine 0x00007fff4b9f14c0 protocol witness for Subscriber.receive(_:) in conformance Subscribers.Sink<A, B> + 16
15 Combine 0x00007fff4ba73ed0 Publishers.HandleEvents.Inner.receive(_:) + 129
16 Combine 0x00007fff4ba74170 protocol witness for Subscriber.receive(_:) in conformance Publishers.HandleEvents<A>.Inner<A1> + 16
17 Combine 0x00007fff4ba26440 closure #1 in Publishers.ReceiveOn.Inner.receive(_:) + 167
18 libswiftDispatch.dylib 0x000000010e97cad0 thunk for #escaping #callee_guaranteed () -> () + 14
19 libdispatch.dylib 0x00007fff20105323 _dispatch_call_block_and_release + 12
20 libdispatch.dylib 0x00007fff20106500 _dispatch_client_callout + 8
21 libdispatch.dylib 0x00007fff2010c12e _dispatch_lane_serial_drain + 715
22 libdispatch.dylib 0x00007fff2010cde1 _dispatch_lane_invoke + 403
23 libdispatch.dylib 0x00007fff20117269 _dispatch_workloop_worker_thread + 782
24 libsystem_pthread.dylib 0x00007fff6116391b _pthread_wqthread + 290
25 libsystem_pthread.dylib 0x00007fff61162b68 start_wqthread + 15
Fatal access conflict detected.
According to Apple, receive(on:options:) runs callbacks on a given queue.
Not exactly. Here's what the documentation actually says:
You use the receive(on:options:) operator to receive results and completion on a specific scheduler, such as performing UI work on the main run loop. In contrast with subscribe(on:options:), which affects upstream messages, receive(on:options:) changes the execution context of downstream messages.
(Emphasis added.) So receive(on:) controls the Scheduler used to call a Subscriber's receive(_:) and receive(completion:) methods. It does not control the Scheduler used to call the Subscription's request(_:) or cancel() methods.
To control the Scheduler used to call the Subscription's cancel() method, you need to use the subscribe(on:options:) operator downstream of the handleEvents operator, like this:
self.localOptionalCancellable = Just(())
.setFailureType(to: Error.self)
.receive(on: self.dispatchQueue)
.handleEvents(
receiveCancel: {
// Simultaneous accesses to 0x600000364e10, but modification requires exclusive access.
// Can be fixed by wrapping in self.dispatchQueue.async {}
self.localOptionalCancellable = nil
}
)
.subscribe(on: self.dispatchQueue)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.sink(
receiveCompletion: { _ in },
receiveValue: { _ in
self.localOptionalCancellable = nil
}
)

DSPGraph when Using CoreML

I am trying to use a SoundAnalyisis model that takes in audioSamples(Float32 15600) and returns a vggishFeature (MultiArray (Float32 12288) however I receive this error:
-01-22 10:45:43.404715+0000 SRTester[25654:891998] [DSPGraph] throwing DSPGraph::Exception with backtrace:
0 0x7fff2bdc0df9 DSPGraph::Graph::processMultiple(DSPGraph::GraphIOData*, DSPGraph::GraphIOData*) + 249
1 0x7fff2bd2223d SoundAnalysis::primeGraph(DSPGraph::Graph&, int) + 542
2 0x7fff2bcfbaae -[SNSoundClassifier primeGraph] + 134
3 0x7fff2bd052c2 -[SNAnalyzerHost primeAnalyzerGraph] + 88
4 0x7fff2bd0f268 -[SNAudioStreamAnalyzer configureAnalysisTreeWithFormat:] + 263
5 0x7fff2bd0f74b -[SNAudioStreamAnalyzer _analyzeAudioBuffer:atAudioFramePosition:] + 303
6 0x10af1cd48 _dispatch_client_callout + 8
7 0x10af2b9bf _dispatch_lane_barrier_sync_invoke_and_complete + 132
8 0x7fff2bd0f5d8 -[SNAudioStreamAnalyzer analyzeAudioBuffer:atAudioFramePosition:] + 121
9 0x10abb3116 $s8SRTester18homeViewControllerC16startAudioEngine33_CDAAA73F093090436FCAC2E152DEFC64LLyyFySo16AVAudioPCMBufferC_So0M4TimeCtcfU_yycfU_ + 326
10 0x10abb315d $sIeg_IeyB_TR + 45
11 0x10af1bdd4 _dispatch_call_block_and_release + 12
12 0x10af1cd48 _dispatch_client_callout + 8
13 0x10af235ef _dispatch_lane_serial_drain + 788
14 0x10af2417f _dispatch_lane_invoke + 422
15 0x10af2fa4e _dispatch_workloop_worker_thread + 719
[truncated?]
libc++abi.dylib: terminating with uncaught exception of type DSPGraph::Exception
(lldb)
The code throws the error in this line:
self.analyzer.analyze(buffer, atAudioFramePosition: time.sampleTime)
which belongs to this block of code:
/// Starts the audio engine
private func startAudioEngine() {
self.isLisitingForInferance = true
//requests to use the engine
do {
let request = try SNClassifySoundRequest(mlModel: soundClassifier.model)
try analyzer.add(request, withObserver: resultsObserver) // sets the results observator
} catch {
print("Unable to prepare request: \(error.localizedDescription)")
return
}
//starts a async task for the analyser
audioEngine.inputNode.installTap(onBus: 0, bufferSize: 16000, format: inputFormat) { buffer, time in
self.analysisQueue.async {
self.analyzer.analyze(buffer, atAudioFramePosition: time.sampleTime) //this line recives a SIGABRT
}
}
do{
try audioEngine.start()
}catch( _){
print("error in starting the Audio Engine")
}
}
Here is the class obsivation (although it does not even get triggered:
class ResultsObserver: NSObject, SNResultsObserving {
var delegate: iPhoneSpeakerRecongitionDelegate?
func request(_ request: SNRequest, didProduce result: SNResult) {
guard let result = result as? SNClassificationResult,
let classification = result.classifications.first else { return }
//print("here")
let confidence = classification.confidence * 100.0
//print(classification.)
if confidence > 60 {
delegate?.displayPredictionResult(identifier: classification.identifier, confidence: confidence)
}
}
}
Managed to get this to return a different error (which is where the model was incompatible)
To resolve this you have to manually move the model file into the app directory and then add it to xcode - it seems to be a bug in Xcode putting the model that is stored in another directory into the app package

Swift compiler segmentation fault after Swift 3 / Xcode 8 port

I have checked multiple answers here in SO, but no solution seems to work for me. When compiling my iOS (≥9.3) app, the following compiler error appears since I converted my project to Swift 3 / Xcode 8.
I tried to clean, delete DerivedData, rebuild Carthage frameworks, etc. - but nothing worked - yet.
Maybe somebody who has experienced this before can immediately spot the problem.
Console output / Stacktrace :
0 swift 0x000000010bc8cb6d PrintStackTraceSignalHandler(void*) + 45
1 swift 0x000000010bc8c5b6 SignalHandler(int) + 470
2 libsystem_platform.dylib 0x00007fffd300dbba _sigtramp + 26
3 libsystem_platform.dylib 000000000000000000 _sigtramp + 754918496
4 swift 0x00000001090c98d2 llvm::Value* llvm::function_ref<llvm::Value* (unsigned int)>::callback_fn<swift::irgen::emitArchetypeWitnessTableRef(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::ArchetypeType>, swift::ProtocolDecl*)::$_0>(long, unsigned int) + 530
5 swift 0x00000001091a7600 swift::irgen::emitImpliedWitnessTableRef(swift::irgen::IRGenFunction&, llvm::ArrayRef<swift::irgen::ProtocolEntry>, swift::ProtocolDecl*, llvm::function_ref<llvm::Value* (unsigned int)> const&) + 240
6 swift 0x00000001090c96a7 swift::irgen::emitArchetypeWitnessTableRef(swift::irgen::IRGenFunction&, swift::CanTypeWrapper<swift::ArchetypeType>, swift::ProtocolDecl*) + 247
7 swift 0x00000001091a39cd swift::SILWitnessVisitor<(anonymous namespace)::WitnessTableBuilder>::visitProtocolDecl(swift::ProtocolDecl*) + 5997
8 swift 0x00000001091a14d7 swift::irgen::IRGenModule::emitSILWitnessTable(swift::SILWitnessTable*) + 503
9 swift 0x00000001091138ed swift::irgen::IRGenerator::emitGlobalTopLevel() + 2077
10 swift 0x00000001091d4fcb performIRGeneration(swift::IRGenOptions&, swift::ModuleDecl*, swift::SILModule*, llvm::StringRef, llvm::LLVMContext&, swift::SourceFile*, unsigned int) + 1259
11 swift 0x00000001090a31c7 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 23687
12 swift 0x000000010909b265 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029
13 swift 0x000000010905882d main + 8685
14 libdyld.dylib 0x00007fffd2e01255 start + 1
15 libdyld.dylib 0x0000000000000067 start + 757067283
And the problematic file / class is the following:
class JSONDataProvider<Input, Output, APIInformation>: DataProvider
where APIInformation: APIAccessInformation, APIInformation.Input == Input,
Output: JSONDataType, Input: Equatable, Input: SignificanceComparable
{
static var updateCallbacksWithoutInputChange: Bool { return false }
// MARK: - Protocol Variables
var callbackId: Int = 0
var callbacks: [Int : (Result<(Output, Input)>) -> ()]
var inputCache: Input?
var outputCache: Result<(Output, Input)>?
// MARK: - Private Properties
fileprivate let apiInformation: APIInformation
fileprivate let requestHandler: URLRequestHandler
// MARK: - Init
init(apiInformation: APIInformation, requestHandler: URLRequestHandler)
{
self.apiInformation = apiInformation
self.requestHandler = requestHandler
self.callbacks = [:]
}
}
For context: (leaving out extensions)
protocol DataProvider
{
associatedtype Input: Equatable, SignificanceComparable
associatedtype Output
static var updateCallbacksWithoutInputChange: Bool { get }
var inputCache: Input? { get set }
var outputCache: Result<(Output, Input)>? { get set }
var callbackId: Int { get set }
var callbacks: [Int : (Result<(Output, Input)>) -> Void] { get set }
func fetchData(from input: Input)
mutating func registerCallback(_ callback: #escaping (Result<(Output, Input)>) -> Void) -> Int
mutating func unregisterCallback(with id: Int)
}
And:
protocol APIAccessInformation
{
associatedtype Input: Equatable, SignificanceComparable
var requestMethod: Alamofire.HTTPMethod { get }
var baseURL: String { get }
func apiParameters(for input: Input) -> [String : Any]
}
Any ideas? Thank you!
Turns out the generic type was the problem. As nobody seems to know exactly why, I have adapted my architecture so I don't need this generic type.
I'm leaving this question here in case others run into a similar issue or someone who knows the exact reason and fix finds this thread.

Crash on iOS 8 when dispatch_group_wait() return because of timeout

I have two operations fetching a value. I only care about the sum both values. And I don't care about the value at all if it takes too long.
So I thought it would be an easy task for GCD using groups. Unfortunately, the below code only works fine on iOS 9. Each time I have no matching calls of dispatch_group_enter()/dispatch_group_leave() I get a crash.
The documentation states clearly that I have to match both call. But when I use a timeout on dispatch_group_wait(), it is impossible to have the same amount of leave calls as enter calls; that is the whole point of specifying a timeout.
So is this a known bug in iOS 8? Am I doing something wrong? Is there another solution to my initial problem that works on iOS 8 as well?
EDIT: Actually we can boil it down to this:
var sync_group: dispatch_group_t = dispatch_group_create();
dispatch_group_enter(sync_group);
let maxWait = dispatch_time(DISPATCH_TIME_NOW, Int64(60 * NSEC_PER_SEC))
let result = dispatch_group_wait(sync_group, maxWait)
sync_group = dispatch_group_create();
Works as expected on iOS 9 but does crash on iOS 8 on the last line because the old dispatch_group_t instance can not be release. Any easy workarounds?
EDIT 2: Turns out it is broken on iOS 9.0 too. It only works as as it should in iOS 9.1+.
Original Code:
let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
/* We want this method to block until the network code succeeded. */
let sync_group: dispatch_group_t = dispatch_group_create();
/* Start async operation 1. */
dispatch_group_enter(sync_group);
self.someAsyncOperation1({(value: Int, finalValue: Bool) in
if (finalValue) {
valOp1 = value
dispatch_group_leave(sync_group);
}
})
/* Start async operation 2. */
dispatch_group_enter(sync_group);
self.someAsyncOperation2({(value: Int, finalValue: Bool) in
if (finalValue) {
valOp2 = value
dispatch_group_leave(sync_group)
}
})
/* Block current thread until all leaves were called. If it takes more then 60 sec we don't care and let go. */
let maxWait = dispatch_time(DISPATCH_TIME_NOW, Int64(60 * NSEC_PER_SEC))
let result = dispatch_group_wait(sync_group, maxWait)
if (result > 0) {
/* This will result in a crash when we leave the scope: SIGTRAP in dispatch_semaphore_dispose */
return
}
dispatch_async(dispatch_get_main_queue(), {
let newValue = valOp1 + valOp2
self.lastKnownNotificationCombinedCounter = newValue
success(newValue)
})
})
The actual crash loops like that:
Exception Type: SIGTRAP
Exception Codes: #0 at 0x3958a2a4
Thread 2 Crashed:
0 libdispatch.dylib 0x3958a2a4 _dispatch_semaphore_dispose$VARIANT$mp + 48
1 libdispatch.dylib 0x3958b491 _dispatch_dispose$VARIANT$mp + 30
2 libdispatch.dylib 0x3957ea8f -[OS_dispatch_object _xref_dispose] + 44
3 myApp 0x00176a24 block_destroy_helper67 + 354
4 myApp 0x00176ab8 0x2e000 + 1346232
5 myApp 0x00178334 0x2e000 + 1352500
6 libsystem_blocks.dylib 0x395d3adb _Block_release + 216
7 Foundation 0x2c4143b9 -[NSBlockOperation dealloc] + 58
8 libobjc.A.dylib 0x39036d57 objc_object::sidetable_release(bool) + 164
9 libobjc.A.dylib 0x390371a9 (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 402
10 libdispatch.dylib 0x39589423 _dispatch_root_queue_drain + 1176
11 libdispatch.dylib 0x3958a1fb _dispatch_worker_thread3 + 104
12 libsystem_pthread.dylib 0x396fae25 _pthread_wqthread + 666
13 libsystem_pthread.dylib 0x396fab78 start_wqthread + 6
I came up with this workaround:
private let MAX_TRIES = 20;
func dispatch_group_wait_ios8Safe(group: dispatch_group_t, _ timeout: dispatch_time_t) -> Int {
if #available(iOS 9, *) {
/* Just forward the call. */
return dispatch_group_wait(group, timeout)
} else {
/* Forward the call to original function and store result. */
let firstResult = dispatch_group_wait(group, timeout)
var result = firstResult, tries = 0
while(result > 0 && tries < MAX_TRIES) {
dispatch_group_leave(group)
result = dispatch_group_wait(group, DISPATCH_TIME_NOW)
tries += 1
}
/* Return original result. */
return firstResult
}
}
So until someone comes up with a better solution I stick with this.

HockeyApp crash, forked Queues, iOS, Swift

Using someone else's framework from GitHub for a UIButton Process effect.
Works fine when installed through latest XCode, but app crashes as soon as button starts animation, if installed through HockeyApp.
Here is the Animation Function in question:
private func startAnimating() {
isAnimating = true
views = []
for i in 0..<colors.count {
let view = UIView(frame: lineRect())
view.backgroundColor = colors[i]
views.append(view)
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
var count: Int = 0
while self.isAnimating {
if count == self.views.count {
count = 0
}
var next = false
dispatch_async(dispatch_get_main_queue(), {
UIView.animateWithDuration(self.duration, delay: 0, options: [], animations: { () -> Void in
if self.isAnimating {
if !self.views.isEmpty {
self.addSubview(self.views[count])
self.views[count].frame.origin = CGPoint(x: self.bounds.origin.x, y: 0)
self.views[count].frame.size.width = self.frame.width
}
}
}, completion: { (Bool) -> Void in
if self.isAnimating {
var lastIndex = count - 1
if lastIndex < 0 {
lastIndex = self.colors.count - 1
}
self.views[lastIndex].frame = self.lineRect()
self.views[lastIndex].removeFromSuperview()
}
next = true
})
})
// Let's wait until the current animation is done before moving forward
while !next {
}
count++
}
})
}
HockeyApp points to the first reference of 'views' inside the second Queue, which in this case is the empty check ( I've added that, but if not there it will point to the next reference ) :
UIView.animateWithDuration(self.duration, delay: 0, options: [], animations: { () -> Void in
if self.isAnimating {
if !self.views.isEmpty {
self.addSubview(self.views[count])
self.views[count].frame.origin = CGPoint(x: self.bounds.origin.x, y: 0)
self.views[count].frame.size.width = self.frame.width
}
}
Here is the key piece of the HockeyApp's crash report:
Date/Time: 2016-02-03T18:01:01Z
Launch Time: 2016-02-03T18:00:33Z
OS Version: iPhone OS 9.2.1 (13D15)
Report Version: 104
Exception Type: SIGTRAP
Exception Codes: #0 at 0x100099080
Crashed Thread: 0
Application Specific Information:
Selector name found in current argument registers: release
Thread 0 Crashed:
0 barscan1 0x0000000100099080 barscan1.ProcessView.((startAnimating in _126B4789AED4AC2C363037724C3D4FEF) (barscan1.ProcessView) -> () -> ()).(closure #1).(closure #1).(closure #1) (ProcessView.swift:78)
1 UIKit 0x0000000185eb8210 +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 616
2 UIKit 0x0000000185ecfc58 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 104
3 barscan1 0x0000000100098d90 barscan1.ProcessView.((startAnimating in _126B4789AED4AC2C363037724C3D4FEF) (barscan1.ProcessView) -> () -> ()).(closure #1).(closure #1) (ProcessView.swift:94)
4 libdispatch.dylib 0x0000000180be1630 _dispatch_call_block_and_release + 20
5 libdispatch.dylib 0x0000000180be15f0 _dispatch_client_callout + 12
6 libdispatch.dylib 0x0000000180be6cf8 _dispatch_main_queue_callback_4CF + 1840
7 CoreFoundation 0x0000000181144bb0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
8 CoreFoundation 0x0000000181142a18 __CFRunLoopRun + 1624
9 CoreFoundation 0x0000000181071680 CFRunLoopRunSpecific + 380
10 GraphicsServices 0x0000000182580088 GSEventRunModal + 176
11 UIKit 0x0000000185ee8d90 UIApplicationMain + 200
12 barscan1 0x00000001000aa32c main (AppDelegate.swift:15)
13 ??? 0x0000000180c128b8 0x0 + 0
Any help is greatly appreciated !
Thanks !
Turned out this is a problem with Apple's Swift Optimization - set it to NONE and now the problem is gone.

Resources