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

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.

Related

compiler-generated crash in Swift in my react-native app

My healthkit swift code keeps crashing in the background in production, and I can't figure it out for the life of me. I am fairly new to Swift, so maybe I am making a fundamental error in my implementation.
Features of the crash:
It seems to happen only in production. (our internal test program contains 10 devices only (so maybe it is co-incidence that it doesn't get picked up there).
Occurs in iOS versions - 10,11,12,13
Occurs for a very small set of users (1.5% of active audience), but very frequently for these same users.
Please find the crash log from my Crashlytics account below.
Crashed: com.facebook.react.HKManagerQueue
0 stepapp 0x100f4f324 specialized HKManager.getTotal(_:typeStr:unit:options:completion:) + 4372984612 (<compiler-generated>:4372984612)
1 stepapp 0x100f52e04 HKManager._getTotals(_:completion:) + 397 (HKManager.swift:397)
2 stepapp 0x100f532ac #objc HKManager.getTotals(_:resolver:rejecter:) + 4373000876 (<compiler-generated>:4373000876)
3 CoreFoundation 0x1af698c20 __invoking___ + 144
4 CoreFoundation 0x1af568d30 -[NSInvocation invoke] + 300
5 CoreFoundation 0x1af569908 -[NSInvocation invokeWithTarget:] + 76
6 stepapp 0x101184e6c -[RCTModuleMethod invokeWithBridge:module:arguments:] + 241556
7 stepapp 0x101187248 facebook::react::invokeInner(RCTBridge*, RCTModuleData*, unsigned int, folly::dynamic const&) + 250736
8 stepapp 0x101186fac invocation function for block in facebook::react::RCTNativeModule::invoke(unsigned int, folly::dynamic&&, int) + 250068
9 libdispatch.dylib 0x1af35e610 _dispatch_call_block_and_release + 24
10 libdispatch.dylib 0x1af35f184 _dispatch_client_callout + 16
11 libdispatch.dylib 0x1af30b404 _dispatch_lane_serial_drain$VARIANT$mp + 608
12 libdispatch.dylib 0x1af30bdf8 _dispatch_lane_invoke$VARIANT$mp + 420
13 libdispatch.dylib 0x1af315314 _dispatch_workloop_worker_thread + 588
14 libsystem_pthread.dylib 0x1af3aeb88 _pthread_wqthread + 276
15 libsystem_pthread.dylib 0x1af3b1760 start_wqthread + 8
com.apple.main-thread
0 libsystem_kernel.dylib 0x19c960634 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x19c95faa0 mach_msg + 72
2 CoreFoundation 0x19cb08288 __CFRunLoopServiceMachPort + 216
3 CoreFoundation 0x19cb033a8 __CFRunLoopRun + 1444
4 CoreFoundation 0x19cb02adc CFRunLoopRunSpecific + 464
5 GraphicsServices 0x1a6aa3328 GSEventRunModal + 104
6 UIKitCore 0x1a0c1063c UIApplicationMain + 1936
7 stepapp 0x100edf330 main + 14 (main.m:14)
8 libdyld.dylib 0x19c98c360 start + 4
com.apple.uikit.eventfetch-thread
0 libsystem_kernel.dylib 0x19c960634 mach_msg_trap + 8
1 libsystem_kernel.dylib 0x19c95faa0 mach_msg + 72
2 CoreFoundation 0x19cb08288 __CFRunLoopServiceMachPort + 216
3 CoreFoundation 0x19cb033a8 __CFRunLoopRun + 1444
4 CoreFoundation 0x19cb02adc CFRunLoopRunSpecific + 464
5 Foundation 0x19ce42784 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 228
6 Foundation 0x19ce42664 -[NSRunLoop(NSRunLoop) runUntilDate:] + 88
7 UIKitCore 0x1a0ca8e80 -[UIEventFetcher threadMain] + 152
8 Foundation 0x19cf7309c __NSThread__start__ + 848
9 libsystem_pthread.dylib 0x19c8a5d8c _pthread_start + 156
10 libsystem_pthread.dylib 0x19c8a976c thread_start + 8
I have attached my implementation below, which contains the line mentioned in the crash logs
func _getTotals(_ options: Dictionary<String, Any>, completion: #escaping (Dictionary<String, Double>?) -> Void) {
var stepsDone = false;
var distanceDone = false;
var caloriesDone = false;
let steps = HKQuantityType.quantityType(forIdentifier: .stepCount);
let distance = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning);
let calories = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned);
var results = Dictionary<String, Double>();
// 👇 THIS IS LINE 397 which is indicated in the crash report above
self.getTotal(steps!, typeStr: HKManager.STEP_TYPE_STR, unit: HKUnit.count(), options: options) { (totalSteps, error) in
stepsDone = true;
if (totalSteps != nil) {
results["steps"] = totalSteps;
}
if (stepsDone == true && distanceDone == true && caloriesDone == true) {
return completion(results);
}
}
self.getTotal(distance!, typeStr: HKManager.DISTANCE_TYPE_STR, unit: HKUnit.meter(), options: options) { (totalDistance, error) in
distanceDone = true;
if (totalDistance != nil) {
results["distance"] = totalDistance;
}
if (stepsDone == true && distanceDone == true && caloriesDone == true) {
return completion(results);
}
}
self.getTotal(calories!, typeStr: HKManager.CALORIES_TYPE_STR, unit: HKUnit.kilocalorie(), options: options) { (totalCalories, error) in
caloriesDone = true;
if (totalCalories != nil) {
results["calories"] = totalCalories;
}
if (stepsDone == true && distanceDone == true && caloriesDone == true) {
return completion(results);
}
}
}
I have also attached my implementation of the self.getTotal(...) function which is used in the above code. Point to note in this function, I switch to performing my HealthKit query in the background qos to ensure that these queries don't run on the main thread. I think it might be the cause for the crash.
func getTotal(_ type: HKQuantityType, typeStr: String, unit: HKUnit, options: Dictionary<String, Any>, completion: #escaping (Double?, Error?) -> Void) {
guard (self.healthStore != nil) else {
let error = NSError(domain: "Healthkit not initialized", code: 50, userInfo: [:]);
return completion(nil, error);
}
var start: Date;
if (options["startDate"] != nil) {
start = self.strToDate(dateStr: options["startDate"] as! String);
} else {
let date = Date()
let cal = Calendar(identifier: .gregorian)
let midnight = cal.startOfDay(for: date);
start = midnight;
}
var ignoreMin = false;
if (options["ignoreMin"] != nil) {
ignoreMin = options["ignoreMin"] as! Bool;
}
if (ignoreMin != true && start < self.minStartDate && self.minStartDate != nil) {
start = self.minStartDate;
}
var end: Date = Date();
if (options["endDate"] != nil) {
end = self.strToDate(dateStr: options["endDate"] as! String);
}
var sources = options["sources"] as? [String];
if (sources == nil || (sources?.capacity)! < 1) {
sources = ["com.apple.health."];
}
DispatchQueue.global(qos: .background).async { [weak self] in
// fetch sources
self?.getSources(sampleTypeStr: typeStr, sampleType: type, sources: sources!) { (s, error) in
if (s == nil || ((s?.capacity) ?? 0) < 1) {
return completion(0.0, nil);
}
let sourcePredicate = HKQuery.predicateForObjects(from: s!);
// todo: enter date patterns
let datePredicate = HKQuery.predicateForSamples(withStart: start, end: end, options: []);
// predicate = [NSPredicate predicateWithFormat:#"metadata.%K != YES", HKMetadataKeyWasUserEntered];
let manualPredicate = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyWasUserEntered, operatorType: .notEqualTo, value: "YES");
let compound = NSCompoundPredicate(andPredicateWithSubpredicates: [
sourcePredicate,
datePredicate,
manualPredicate
]);
let statOptions = HKStatisticsOptions.cumulativeSum;
let query = HKStatisticsQuery.init(quantityType:type , quantitySamplePredicate: compound, options: statOptions, completionHandler: { (query, results, error) in
if (error != nil) {
return completion(nil, error);
}
var total = 0.0;
// handle if results came back as nil, or sum came back as nil
guard (results != nil && results?.sumQuantity() != nil) else {
return completion(total, nil);
}
total = results?.sumQuantity()?.doubleValue(for: unit) ?? 0.0;
return completion(total, nil);
});
// execute stats query for step counts by source
self?.healthStore?.execute(query);
}
}
}
I would really appreciate any form of help, or pointers. Thanks in advance.
The obvious issue is parallel write from several threads to dictionary object. In pseudo code:
results = [:]
getTotal() // start thread 1
getTotal() // start thread 2
getTotal() // start thread 3
thread 1: write results
thread 2: write results
thread 3: write results
Swift Dictionary is not thread-safe, parallel writes have to be synchronized.
In your code, simple change would be to move DispatchQueue async up, into _getTotals, and removing DispatchQueue from getTotal:
func _getTotals(_ options: Dictionary<String, Any>, completion: #escaping (Dictionary<String, Double>?) -> Void) {
DispatchQueue.global(qos: .background).async {
var results = Dictionary<String, Double>()
self.getTotal(...)
self.getTotal(...)
self.getTotal(...)
}
}
In this way everything runs inside single background thread; getTotal calls are invoked serially one after another.
But if you need to run getTotal in parallel, you have to synchronize access to results variable. This is commonly done with another call to DispatchQueue async with a predefined shared serial queue.

Firebase crashlytics reports many insight available crashes in my app

Below is my code
func getImageFrom(_ asset: PHAsset, completion: #escaping (UIImage) -> Void) {
var thumbnail = UIImage()
let imageManager = PHCachingImageManager()
let options = PHImageRequestOptions()
options.version = .current
let size = CGSize(width: 150.0, height: 150.0)
imageManager.requestImage(for: asset, targetSize: size, contentMode: .aspectFill,
options: nil) { (image, info) in
guard let img = image else { return }
thumbnail = img
}
completion(thumbnail)
}
I'm using this function to get an Image from PHAsset.
some time crash is reported at
thumbnail = img or at completion(thumbnail).
using this function in below code block
func getImageDataAndFindDupImages() {
var i = 0
var similarImages = [OSTuple<NSString, NSString>]()
similarImages = OSImageHashing.sharedInstance().similarImages(with: .high) { () -> OSTuple<NSString, NSData>? in
#if DEBUG
print(i)
#endif
var tuple = OSTuple<NSString, NSData>()
if self.tempArrAssets.count > 0 {
DispatchQueue.main.async {
self.lblNumberOfPhoto.text = R.string.localizable.scanning_photos_number("\(i+1)", "\(self.arrPhotoAssets.count)")
}
if !self.tempArrAssets.isEmpty {
let tempArrAsset = self.tempArrAssets.removeFirst()
self.getImageFrom(tempArrAsset) { (imageAsset) in
self.arrImageFromAsset.append(imageAsset)
if let imgData = imageAsset.pngData() {
tuple = (OSTuple<NSString, NSData>(first: NSString(string: "\(i)"), andSecond: imgData as NSData))
}
}
i += 1
return tuple
} else {
return nil
}
} else {
return nil
}
}
print("To make dup tuples:", Date().timeIntervalSince(self.date))
}
I'm using getImageFrom in getImageDataAndFindDupImages
This is a log for the crash
Crashed: com.apple.mobileslideshow.accessCallbacks
0 libobjc.A.dylib 0x18a874020 objc_retain + 16
1 Duplicate Cleaner 0x1001f0e80 closure #1 in DuplicatePhotoFinderVC.getImageDataAndFindDupImages() + 519 (DuplicatePhotoFinderVC.swift:519)
2 Duplicate Cleaner 0x1001f16a4 thunk for #escaping #callee_guaranteed () -> (#owned OSTuple<NSString, NSData>?) + 4338357924 (<compiler-generated>:4338357924)
3 CocoaImageHashing 0x1004f3514 -[OSSimilaritySearch similarImagesWithProvider:withHashDistanceThreshold:forImageStreamHandler:forResultHandler:] + 46 (OSSimilaritySearch.m:46)
4 CocoaImageHashing 0x1004f3924 -[OSSimilaritySearch similarImagesWithProvider:withHashDistanceThreshold:forImageStreamHandler:] + 95 (OSSimilaritySearch.m:95)
5 CocoaImageHashing 0x1004f28d0 -[OSImageHashing similarImagesWithProvider:withHashDistanceThreshold:forImageStreamHandler:] + 222 (OSImageHashing.m:222)
6 CocoaImageHashing 0x1004f27b0 -[OSImageHashing similarImagesWithHashingQuality:withHashDistanceThreshold:forImageStreamHandler:] + 201 (OSImageHashing.m:201)
7 CocoaImageHashing 0x1004f2734 -[OSImageHashing similarImagesWithHashingQuality:forImageStreamHandler:] + 190 (OSImageHashing.m:190)
8 Duplicate Cleaner 0x1001ee63c DuplicatePhotoFinderVC.getImageDataAndFindDupImages() + 316 (DuplicatePhotoFinderVC.swift:316)
9 Duplicate Cleaner 0x1001ee434 DuplicatePhotoFinderVC.fetchPhotosandCheckforDuplicate() + 146 (DuplicatePhotoFinderVC.swift:146)
10 Duplicate Cleaner 0x1001eea1c closure #1 in DuplicatePhotoFinderVC.checkForPermissionAndScanPhotos() + 162 (DuplicatePhotoFinderVC.swift:162)
11 Duplicate Cleaner 0x10021bdec thunk for #escaping #callee_guaranteed (#unowned PHAuthorizationStatus) -> () + 4338531820 (<compiler-generated>:4338531820)
12 Photos 0x195ff9098 __39+[PHPhotoLibrary requestAuthorization:]_block_invoke + 64
13 AssetsLibraryServices 0x19f048edc __79-[PLPrivacy _isPhotosAccessAllowedWithScope:forceHandler:accessAllowedHandler:]_block_invoke.14 + 520
14 AssetsLibraryServices 0x19f01486c __pl_dispatch_async_block_invoke + 36
15 libdispatch.dylib 0x18a7fe610 _dispatch_call_block_and_release + 24
16 libdispatch.dylib 0x18a7ff184 _dispatch_client_callout + 16
17 libdispatch.dylib 0x18a7ab404 _dispatch_lane_serial_drain$VARIANT$mp + 608
18 libdispatch.dylib 0x18a7abdf8 _dispatch_lane_invoke$VARIANT$mp + 420
19 libdispatch.dylib 0x18a7b5314 _dispatch_workloop_worker_thread + 588
20 libsystem_pthread.dylib 0x18a84eb88 _pthread_wqthread + 276
21 libsystem_pthread.dylib 0x18a851760 start_wqthread + 8
I'm not able to understand why I'm getting these crashes. If you have any knowledge please guide me.
You are passing nil to options parameter in the imageManager.requestImage(for:targetSize: contentMode:options:). If you pass nil as options, the method calls the completion block asyncrohonously. So there is a chance by the time your completion block gets called the method getImageFrom(: completion:) has exited and the local variable thumbnail has been destroyed(I am not talking about the UIImage object being destroyed but the variable which holds address of the UIImage itself). Then trying to set the thumbnail varibale which is no more on the stack, inside the block might be the reason there is a crash.
To solve your problem Create an instance of PHImageRequestOptions set its isSynchronous property to true like so.
let options = PHImageRequestOptions()
options.isSynchronous = true
Now pass this options as a parameter to the imageManager.requestImage(for:targetSize: contentMode:options:)
As a result your imageManager.requestImage(for:targetSize: contentMode:options:) will call the block synchronously meaning before the getImageFrom(: completion:) method exits and so the variable thumbnail is still alive on the stack to be used.
Hoping this should solve your problem.
PS: Take care you call the method getImageFrom(: completion:) in a background thread or your main thread will be frozen

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

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.

Why my [UIScrollView removeFromSuperview] is crashing?

The crash log is below.
Do you know any particular reason why might [UIScrollView removeFromSuperview] can crash? The scrollview contains view hierarchy with different types of UIViews. I also finds that the ad hoc version crash often not the debug version. I could not find any reason for that.
Same viewcontroller is loaded in a different flow in iPhone that works fine. But in iPad it crashes.
In iPad, in a container view controller, only viewcontroler.view is loaded.
Incident Identifier: EE102239-34D1-4BE7-8B52-41F74AB26203
CrashReporter Key: 2b11ea2a01ac5618e199ffc5a1e1f321600bb6a9
Hardware Model: iPad3,4
Version: ??? (???)
Code Type: ARM (Native)
Parent Process: launchd [1]
Date/Time: 2013-06-18 15:19:16.132 +0200
OS Version: iOS 6.1.3 (10B329)
Report Version: 104
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000000
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libobjc.A.dylib 0x3bab7070 prepareForMethodLookup + 20
1 libobjc.A.dylib 0x3bab6fb2 lookUpMethod + 42
2 libobjc.A.dylib 0x3bab6f7e _class_lookupMethodAndLoadCache3 + 14
3 libobjc.A.dylib 0x3bab6638 objc_msgSend_uncached + 24
4 QuartzCore 0x357f2a72 CA::Layer::contents_visibility_changed(CA::Transaction*, bool) + 50
5 QuartzCore 0x357f29de CA::Layer::mark_visible(CA::Transaction*, bool) + 190
6 QuartzCore 0x357f29b2 CA::Layer::mark_visible(CA::Transaction*, bool) + 146
7 QuartzCore 0x357f29b2 CA::Layer::mark_visible(CA::Transaction*, bool) + 146
8 QuartzCore 0x357f29b2 CA::Layer::mark_visible(CA::Transaction*, bool) + 146
9 QuartzCore 0x357f29b2 CA::Layer::mark_visible(CA::Transaction*, bool) + 146
10 QuartzCore 0x357f28d2 CA::Layer::update_removed_sublayer(CA::Transaction*, unsigned int) + 18
11 QuartzCore 0x357f255a CA::Layer::remove_sublayer(CA::Transaction*, CALayer*) + 130
12 QuartzCore 0x357f246a CA::Layer::remove_from_superlayer() + 34
13 UIKit 0x35a6e92c -[UIView(Hierarchy) removeFromSuperview] + 144
14 UIKit 0x35b857bc -[UIScrollView removeFromSuperview] + 60
15 MyApp 0x000bde8a -[iPadNavigationController vcAnimationDone] (iPadNavigationController.m:400)
16 UIKit 0x35a55ab6 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 154
17 UIKit 0x35aca8f8 -[UIViewAnimationState animationDidStop:finished:] + 44
18 QuartzCore 0x35801304 CA::Layer::run_animation_callbacks(void*) + 204
19 libdispatch.dylib 0x3bed55d8 _dispatch_client_callout + 20
20 libdispatch.dylib 0x3bed8e40 _dispatch_main_queue_callback_4CF + 224
21 CoreFoundation 0x33c051ac __CFRunLoopRun + 1284
22 CoreFoundation 0x33b78238 CFRunLoopRunSpecific + 352
23 CoreFoundation 0x33b780c4 CFRunLoopRunInMode + 100
24 GraphicsServices 0x37733336 GSEventRunModal + 70
25 UIKit 0x35a942b4 UIApplicationMain + 1116
A few line from the code (as asked),
previous = showing;
showing = [ vc retain ];
showing.view.frame = startFrameIn;
[ container addSubview:showing.view ];
CGContextRef context = UIGraphicsGetCurrentContext();
[ UIView beginAnimations:nil context:context ];
[ UIView setAnimationDelegate:self ];
[ UIView setAnimationDidStopSelector:#selector(vcAnimationDone) ];
[ UIView setAnimationCurve:UIViewAnimationCurveEaseOut ];
[ UIView setAnimationDuration:0.4 ];
previous.view.frame = endFrameOut;
showing.view.frame = detailFrame;
[ UIView commitAnimations ];
}
- (void) vcAnimationDone {
if ( previous != nil ) {
if (previous.view.superview != nil) {
[previous.view removeFromSuperview];
}
[ previous release ];
previous = nil;
}
A very probable reason is that you are overreleasing your scrollview or one of the views inside it.
Calling removeFromSuperview then deallocates the view instead of simply decreasing the retain count.
Actually, if you are still stuck with non-ARC project, Static Code Analysis is very useful for this kind of bug. Retain/release balancing issues are hard to pin down, and nearly impossible with incomplete method so I suggest you post the full method body if possible.
Thanks everyone for your answers, tips and tricks. However one thing I want share with you is the cause of the crash. I found that the crash was at different thread in different times. I had several views loaded with button pressing/menu in my iPad app. Some of the button pressing fetch data from web service. So I was bit confused to get the cuase of crash, animation, or url connection etc... I tried with enabled NSZombie objects, but it did not show any information.
Then I tried with Guard Malloc. This only runs in Simulator. And magically I found the code point of crash. I have function to convert a hex string into data. There I have line of code to make a C string null terminated. where I assigned 0 at the last index. and that makes the crash!
tmpCh[count] = 0;
I do not why, but probably it takes some time in the memory management procedure in iOS so it crash at different thread at different times. But with Guard malloc in Simulator, it always point out here and when I rewrite the code, the crash is gone.
/* Converts a hex string to bytes.
Precondition:
. The hex string can be separated by space or not.
. the string length without space or 0x, must be even. 2 symbols for one byte/char
. sample input: 23 3A F1 OR 233AF1
*/
+ (NSData *) dataFromHexString:(NSString*)hexString
{
if (hexString.length < 1) {
return nil;
}
char * tmpCh = (char *) malloc([hexString length] * sizeof(char));
int count = 0;
for (int k=0; k<hexString.length;k++) {
char c = [hexString characterAtIndex:k];
if (c == (char)0x20) { //skip space
continue;
}
if (c == '0') { // skip 0x
if(k+1 < hexString.length){
if ([hexString characterAtIndex:k+1] == 'x'
|| [hexString characterAtIndex:k+1] == 'X' )
{
k = k + 1;
continue;
}
}
}
tmpCh[count] = c;
count++;
}
tmpCh[count] = 0; // make null terminated string
if (count % 2) {
return nil;
}
NSString *temp = [[NSString alloc] initWithUTF8String:tmpCh];
free(tmpCh);
if ([temp length] % 2 != 0) {
return nil;
}
NSMutableData *result = [[NSMutableData alloc] init];
unsigned char byte;
char hexChars[3] = {0};
for (int i=0; i < (temp.length/2); i++) {
hexChars[0] = [temp characterAtIndex:i*2];
hexChars[1] = [temp characterAtIndex:i*2+1];
if (![Util isValidChar:hexChars[0]] || ![Util isValidChar:hexChars[1]]) {
return nil;
}
byte = strtol(hexChars, NULL, 16);
[result appendBytes:&byte length:1];
}
NSData * data = [NSData dataWithData:result];
[result release];
return data;
}

Resources