Swift malloc_error_break crashes with double free - ios

I'm working on an app that fetches data from a web API. Most of the time, it runs perfectly; however, sometimes I'll received the error:
malloc: *** error for object 0x7fc2b061de30: double free
*** set a breakpoint in malloc_error_break to debug
I've set a malloc_error_break breakpoint, and this shows up when the app crashes, with the first line highlighted:
0x112375760 <+0>: pushq %rbp
0x112375761 <+1>: movq %rsp, %rbp
0x112375764 <+4>: nop
0x112375765 <+5>: nopl (%rax)
0x112375769 <+9>: popq %rbp
0x11237576a <+10>: retq
I have no clue what any of this means. I've searched stack exchange for hours, but I can't get any help. Once, the error showed up as:
pointer being freed was not allocated
Please don't judge me, but I don't know what a pointer is, what it means to free something, or allocate an object. If anyone wants to give me a quick crash course and/or enlighten me as what my error could be, that would be wonderful.
EDIT:
I believe the error may be coming from an UIImageView extension to download an image:
extension UIImageView {
func downloadedFrom(targetURL: NSURL, onCompletion: (UIImage?, NSError?) -> Void) {
print("downloading image")
SpotifyAPIManager.sharedInstance.auth.client.get(targetURL.absoluteString, success: { (data, response) in
print("successfully downloaded image")
guard (response as NSHTTPURLResponse).statusCode == 200 else {
print("error in http response. status code: \((response as NSHTTPURLResponse).statusCode)")
return
}
let image = UIImage(data: data)
onCompletion(image, nil)
}, failure: { error in
print("error while downloading image: \(error)")
})
}
}
(the auth.client.get is a method from the OAuthSwift framework)
I printed out the backtrace after a crash:
* thread #1: tid = 0x2cbf0, 0x00000001089aa760 libsystem_malloc.dylib`malloc_error_break, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x00000001089aa760 libsystem_malloc.dylib`malloc_error_break
frame #1: 0x00000001089a3270 libsystem_malloc.dylib`szone_error + 406
frame #2: 0x00000001089a4f07 libsystem_malloc.dylib`tiny_malloc_from_free_list + 881
frame #3: 0x00000001089a3685 libsystem_malloc.dylib`szone_malloc_should_clear + 294
frame #4: 0x00000001089ab6e6 libsystem_malloc.dylib`malloc_zone_calloc + 78
frame #5: 0x00000001089abe4f libsystem_malloc.dylib`calloc + 49
frame #6: 0x0000000109bb5ee0 ImageIO`initImageAppleJPEG + 971
frame #7: 0x0000000109bbbc9e ImageIO`_CGImagePluginInitAppleJPEG + 76
frame #8: 0x0000000109af7dff ImageIO`makeImagePlus + 1377
frame #9: 0x0000000109af7282 ImageIO`CGImageSourceCreateImageAtIndex + 184
frame #10: 0x0000000106915ef0 UIKit`_UIImageRefFromData + 423
frame #11: 0x00000001066f5086 UIKit`-[UIImage(UIImagePrivate) _initWithData:preserveScale:cache:] + 124
frame #12: 0x00000001042c7874 Spotify`#nonobjc UIImage.init(data : NSData) -> UIImage? + 36 at SongTableViewCell.swift:0
frame #13: 0x00000001042c73dc Spotify`UIImage.__allocating_init(data : NSData) -> UIImage? + 76 at SongTableViewCell.swift:0
frame #14: 0x00000001042c718b Spotify`UIImageView.(data=142201 bytes, response=0x00007fd62a64bd90, onCompletion=0x00000001042c6ab0 Spotify`partial apply forwarder for reabstraction thunk helper from #callee_unowned #convention(block) (#unowned Swift.Optional<__ObjC.UIImage>, #unowned Swift.Optional<__ObjC.NSError>) -> (#unowned ()) to #callee_owned (#owned Swift.Optional<__ObjC.UIImage>, #owned Swift.Optional<__ObjC.NSError>) -> (#unowned ()) at SongTableViewCell.swift) -> ()) -> ()).(closure #1) + 315 at SongTableViewCell.swift:65
frame #15: 0x00000001042bec63 Spotify`thunk + 35 at SpotifyAPIManager.swift:0
frame #16: 0x00000001042c6891 Spotify`partial apply for thunk + 81 at SongTableViewCell.swift:0
frame #17: 0x0000000108087f86 OAuthSwift`OAuthSwiftHTTPRequest.(data=142201 bytes, response=0x00007fd62a64bd90, error=nil, self=0x00007fd62a636411) -> ()).(closure #1).(closure #1) + 5206 at OAuthSwiftHTTPRequest.swift:149
frame #18: 0x0000000108088ae7 OAuthSwift`thunk + 103 at OAuthSwiftHTTPRequest.swift:0
frame #19: 0x0000000104bebb49 CFNetwork`__75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19
frame #20: 0x0000000104bfe0f2 CFNetwork`__49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 302
frame #21: 0x000000010602b630 Foundation`__NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
frame #22: 0x0000000105f66805 Foundation`-[NSBlockOperation main] + 101
frame #23: 0x0000000105f49725 Foundation`-[__NSOperationInternal _start:] + 646
frame #24: 0x0000000105f49336 Foundation`__NSOQSchedule_f + 194
frame #25: 0x00000001087b13eb libdispatch.dylib`_dispatch_client_callout + 8
frame #26: 0x00000001087991ef libdispatch.dylib`_dispatch_main_queue_callback_4CF + 1738
frame #27: 0x00000001054430f9 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
frame #28: 0x0000000105404b99 CoreFoundation`__CFRunLoopRun + 2073
frame #29: 0x00000001054040f8 CoreFoundation`CFRunLoopRunSpecific + 488
frame #30: 0x0000000109fdead2 GraphicsServices`GSEventRunModal + 161
frame #31: 0x0000000106693f09 UIKit`UIApplicationMain + 171
frame #32: 0x00000001042cbe72 Spotify`main + 114 at AppDelegate.swift:14
frame #33: 0x00000001087e592d libdyld.dylib`start + 1
Once again I have no clue what this means but it could help debug.

Without any code to back up your problem it's hard to say exactly what's causing this. As a note of education: Pointers are references to memory allocated on the heap, and many different objects can point to the same memory location. Although Swift doesn't really use pointers, the underlying runtime does, and here, you've tried to access some memory location that the runtime thinks should no longer be allocated within your program's memory. As for the assembly code, that's showing you what's happening in the hardware at the time this is occurring. A brief rundown shows that the hardware is trying to move objects around in memory between the registers, and when it goes to pop to access one of the memory locations dereferenced by what's stored in the register, this is where it crashes. If you can post some code from where you think this might be happening, or from a certain part of the app where it does this frequently, maybe we can be of more help!
Edit: Also as for the actual error message itself: This means that that specific memory location has already been freed (which in this case would mean that the runtime doesn't think you have any more references to any objects that point there, and then it tries to free it again, resulting in the double-free error.
Disclaimer: I know this isn't really an answer, and would be better suited as a comment but it was too long! I wanted to educate OP!

Related

Infinite loop in pthread_from_mach_thread_np freezing app

I am investigating an issue for an iOS app where the main thread appears to be hung. It believe I have reproduced it locally a couple times, but it is not very repeatable. I have attached a few example stack traces below where I have seen this.
I see a dispatch_barrier_sync_f call which “submits a barrier function for execution and waits until that function completes”. This is happening on a number of the other threads which are all waiting on a ulock_wait.
In all of the hangs, there is one thread that has a dispatch_barrier_sync_f which appears to be active. From what we could tell, it is stuck in the function pthread_from_mach_thread_np. I only have assembly code here, but here is some source. It appears to be converting from a mach_thread to a pthread. When I step through the assembly it appears to be stuck in the loop portion of iterating over the pthread list.
It is not clear to me how this fixed loop could hang. Perhaps the memory is corrupted and it is not looping over a valid pthread list?
This may be related to the issue posted here.
I have a few examples of this happening all from different services
Example 1:
thread #104, queue = 'com.apple.coremedia.decompressionsession.clientcallback'
frame #0: 0x00000001f17fc100 libsystem_pthread.dylib`pthread_from_mach_thread_np + 68
frame #1: 0x0000000109139c98 libdispatch.dylib`_dispatch_introspection_continuation_get_info + 244
frame #2: 0x000000010913ae74 libdispatch.dylib`_dispatch_introspection_queue_item_enqueue_hook + 44
frame #3: 0x000000010913a42c libdispatch.dylib`_dispatch_introspection_queue_fake_sync_push_pop + 116
frame #4: 0x0000000109107f1c libdispatch.dylib`_dispatch_barrier_sync_f + 224
frame #5: 0x0000000197286720 VideoToolbox`__vtdsr_dequeueAllPendingFramesAndCallbackClientForEach_block_invoke + 1000
frame #6: 0x0000000109100c6c libdispatch.dylib`_dispatch_call_block_and_release + 32
frame #7: 0x00000001091027bc libdispatch.dylib`_dispatch_client_callout + 20
frame #8: 0x000000010910aa60 libdispatch.dylib`_dispatch_lane_serial_drain + 1428
frame #9: 0x000000010910b5e0 libdispatch.dylib`_dispatch_lane_invoke + 428
frame #10: 0x0000000109118168 libdispatch.dylib`_dispatch_workloop_worker_thread + 908
frame #11: 0x00000001f17f60bc libsystem_pthread.dylib`_pthread_wqthread + 288
Example 2:
thread #97
frame #0: 0x00000001f0b980ec libsystem_pthread.dylib`pthread_from_mach_thread_np + 48
frame #1: 0x00000001059f9c98 libdispatch.dylib`_dispatch_introspection_continuation_get_info + 244
frame #2: 0x00000001059fae74 libdispatch.dylib`_dispatch_introspection_queue_item_enqueue_hook + 44
frame #3: 0x00000001059fa42c libdispatch.dylib`_dispatch_introspection_queue_fake_sync_push_pop + 116
frame #4: 0x00000001059c7f1c libdispatch.dylib`_dispatch_barrier_sync_f + 224
frame #5: 0x0000000187fbbf54 CoreData`_perform + 176
frame #6: 0x0000000187e23a28 CoreData`-[NSPersistentStoreCoordinator _routeLightweightBlock:toStore:] + 204
frame #7: 0x0000000187ea71f4 CoreData`-[NSPersistentStoreCoordinator(_NSInternalMethods) newValuesForObjectWithID:withContext:error:] + 368
frame #8: 0x0000000187e89f7c CoreData`_PFFaultHandlerLookupRow + 296
frame #9: 0x0000000187ec357c CoreData`_PF_FulfillDeferredFault + 208
frame #10: 0x0000000187e25fc8 CoreData`_PF_ManagedObject_WillChangeValueForKeyIndex + 84
frame #11: 0x0000000187e3f260 CoreData`_sharedIMPL_setvfk_core + 152
frame #12: 0x000000010128f408 MyApp`-[GMSTileDataCache touchCachedTile:] + 100
frame #13: 0x000000010128fb7c MyApp`__72-[GMSTileDataCache loadTileForTileCoords:dataVersion:completionHandler:]_block_invoke + 216
frame #14: 0x0000000187e18834 CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 156
frame #15: 0x00000001059c27bc libdispatch.dylib`_dispatch_client_callout + 20
frame #16: 0x00000001059ca8a4 libdispatch.dylib`_dispatch_lane_serial_drain + 984
frame #17: 0x00000001059cb5e0 libdispatch.dylib`_dispatch_lane_invoke + 428
frame #18: 0x00000001059d8168 libdispatch.dylib`_dispatch_workloop_worker_thread + 908
frame #19: 0x00000001f0b920bc libsystem_pthread.dylib`_pthread_wqthread + 288
Example 3:
thread #99, queue = 'flight logger queue'
frame #0: 0x00000001f16040f8 libsystem_pthread.dylib`pthread_from_mach_thread_np + 60
frame #1: 0x0000000105909c98 libdispatch.dylib`_dispatch_introspection_continuation_get_info + 244
frame #2: 0x000000010590b0d0 libdispatch.dylib`_dispatch_introspection_queue_item_dequeue_hook + 44
frame #3: 0x000000010590a440 libdispatch.dylib`_dispatch_introspection_queue_fake_sync_push_pop + 136
frame #4: 0x00000001058d7f1c libdispatch.dylib`_dispatch_barrier_sync_f + 224
frame #5: 0x000000019ac06cac libswiftDispatch.dylib`merged implicit closure #2 (() -> ()) -> () in implicit closure #1 (__C.OS_dispatch_queue) -> (() -> ()) -> () in __C.OS_dispatch_queue.sync<τ_0_0>(execute: () throws -> τ_0_0) throws -> τ_0_0 + 180
frame #6: 0x000000019ac05d0c libswiftDispatch.dylib`partial apply forwarder for implicit closure #2 (() -> ()) -> () in implicit closure #1 (__C.OS_dispatch_queue) -> (() -> ()) -> () in __C.OS_dispatch_queue.sync<τ_0_0>(execute: () throws -> τ_0_0) throws -> τ_0_0 + 56
frame #7: 0x000000019ac069ec libswiftDispatch.dylib`__C.OS_dispatch_queue._syncHelper<τ_0_0>(fn: (() -> ()) -> (), execute: () throws -> τ_0_0, rescue: (Swift.Error) throws -> τ_0_0) throws -> τ_0_0 + 396
frame #8: 0x000000019ac05dbc libswiftDispatch.dylib`__C.OS_dispatch_queue.sync<τ_0_0>(execute: () throws -> τ_0_0) throws -> τ_0_0 + 168
frame #9: 0x0000000100324f8c MyApp`FlightData.flightDataComponents(self=0x000000010645aee0) at FlightData.swift:184:35
frame #10: 0x000000010029c014 MyApp`DDLogDataFactory.generateLogDataComponent(now=2022-06-30 22:54:33 UTC, flightData=0x000000010645aee0, self=0x0000000287465280) at DDLogDataFactory.swift:197:44
frame #11: 0x0000000100364688 MyApp`DDFlightLogFormatterV1.flightData(flightData=0x000000010645aee0, self=0x0000000283461040) at DDFlightLogFormatterV1.swift:45:57
frame #12: 0x0000000100364768 MyApp`#objc DDFlightLogFormatterV1.flightData(_:) at <compiler-generated>:0
frame #13: 0x00000001001d06b4 MyApp`__31-[FlightLogger logCurrentData:]_block_invoke(.block_descriptor=0x0000000283bc01b0) at FlightLogger.m:408:30
frame #14: 0x00000001001d0e40 MyApp`__41-[FlightLogger executeBlockInBackground:]_block_invoke(.block_descriptor=0x0000000283bc22b0) at FlightLogger.m:457:9
frame #15: 0x00000001058d0c6c libdispatch.dylib`_dispatch_call_block_and_release + 32
frame #16: 0x00000001058d27bc libdispatch.dylib`_dispatch_client_callout + 20
frame #17: 0x00000001058da8a4 libdispatch.dylib`_dispatch_lane_serial_drain + 984
frame #18: 0x00000001058db5e0 libdispatch.dylib`_dispatch_lane_invoke + 428
frame #19: 0x00000001058e8168 libdispatch.dylib`_dispatch_workloop_worker_thread + 908
frame #20: 0x00000001f15fe0bc libsystem_pthread.dylib`_pthread_wqthread + 288
Any help would be greatly appreciated, thank you

EXC_BAD_ACCESS error code in Swift

I'm creating a simple note taking iOS app with Swift.
When I attempt to save the location of the device to the note object I get an error of EXC_BAD_ACCESS(code=EXC_I386_GPFLT). I understand that this error is related to the deallocation of memory during run time, but I cant find the issue. I'm using very similar code to perform the same operation elsewhere in the app with success.
I have tried to enable Zombie object with no luck. I have also looked at a number of questions on here plus blog posts, but they did not help me find the issue (but did help me to understand it).
This is the code that the error is thrown in. I have also added the entire (non refactored) class to pastebin incase more context is needed.
var audioNote: AudioNote!
func createAudioNote() {
let appDelegate: AppDelegate = localAppDelegate()
let managedContext: NSManagedObjectContext = appDelegate.managedObjectContext!
let entityDescription:NSEntityDescription! = NSEntityDescription.entityForName("AudioNote", inManagedObjectContext: managedContext)
audioNote = AudioNote(entity: entityDescription, insertIntoManagedObjectContext: managedContext)
audioNote.date_created = NSDate()
audioNote.url = "\(soundFileURL)"
if (appDelegate.userLocation != nil){
println(appDelegate.userLocation!.coordinate.latitude)// <- Contains latitude as expected
println(audioNote)// <- Contains note as expected
audioNote.latitude = appDelegate.userLocation!.coordinate.latitude // <- Error throws here
audioNote.longitude = appDelegate.userLocation!.coordinate.longitude
}
}
The stack trace points me towards and issue with CoreData, but beyond checking the naming of the entity and attributes I'm not sure what to do.
The stack trace is as follows:
* thread #1: tid = 0x581ddd, 0x0000000112ef1017 libobjc.A.dylib`objc_msgSend + 23, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
frame #0: 0x0000000112ef1017 libobjc.A.dylib`objc_msgSend + 23
frame #1: 0x0000000111d751f5 CoreData`_PFManagedObject_coerceValueForKeyWithDescription + 1493
frame #2: 0x0000000111d4e2f1 CoreData`_sharedIMPL_setvfk_core + 177
* frame #3: 0x0000000110087c79 ELLIE`ELLIE.AudioNoteController.createAudioNote (self=<unavailable>)() -> () + 2345 at AudioNoteController.swift:117
frame #4: 0x000000011008601b ELLIE`ELLIE.AudioNoteController.saveAudioNote (sender=AnyObject at 0x00007fff4fc050e8, self=<unavailable>)(Swift.AnyObject) -> () + 59 at AudioNoteController.swift:47
frame #5: 0x0000000110086076 ELLIE`#objc ELLIE.AudioNoteController.saveAudioNote (ELLIE.AudioNoteController)(Swift.AnyObject) -> () + 54 at AudioNoteController.swift:0
frame #6: 0x0000000110a36d62 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 75
frame #7: 0x0000000110b4850a UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 467
frame #8: 0x0000000110b478d9 UIKit`-[UIControl touchesEnded:withEvent:] + 522
frame #9: 0x0000000110a83958 UIKit`-[UIWindow _sendTouchesForEvent:] + 735
frame #10: 0x0000000110a84282 UIKit`-[UIWindow sendEvent:] + 682
frame #11: 0x0000000110a4a541 UIKit`-[UIApplication sendEvent:] + 246
frame #12: 0x0000000110a57cdc UIKit`_UIApplicationHandleEventFromQueueEvent + 18265
frame #13: 0x0000000110a3259c UIKit`_UIApplicationHandleEventQueue + 2066
frame #14: 0x00000001124c3431 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #15: 0x00000001124b92fd CoreFoundation`__CFRunLoopDoSources0 + 269
frame #16: 0x00000001124b8934 CoreFoundation`__CFRunLoopRun + 868
frame #17: 0x00000001124b8366 CoreFoundation`CFRunLoopRunSpecific + 470
frame #18: 0x0000000115c8ea3e GraphicsServices`GSEventRunModal + 161
frame #19: 0x0000000110a358c0 UIKit`UIApplicationMain + 1282
frame #20: 0x0000000110097237 ELLIE`main + 135 at AppDelegate.swift:16
frame #21: 0x0000000113628145 libdyld.dylib`start + 1
frame #22: 0x0000000113628145 libdyld.dylib`start + 1
Any help is much appreciated.
Tony
Check that you've declared your custom NSManagedObject sub-class in your Model schema. This will need to be in the fully qualified APP.ClassName format, e.g:
I'm not sure that it will help, but I'd recommend to rewrite this part:
if (appDelegate.userLocation != nil){
println(appDelegate.userLocation!.coordinate.latitude)// <- Contains latitude as expected
println(audioNote)// <- Contains note as expected
audioNote.latitude = appDelegate.userLocation!.coordinate.latitude // <- Error throws here
audioNote.longitude = appDelegate.userLocation!.coordinate.longitude
}
Like that:
if let location = appDelegate.userLocation{
print(audioNote)
audioNote.latitude = location.coordinate.latitude
audioNote.longitude = location.coordinate.longitude
}

UIDocumentInteractionControllerDelegate EXC_BAD_ACCESS in Swift

I'm currently trying to open a file with another application using an instance of UIDocumentInteractionController. Everything works fine (e.g. showing the options dialog, opening the application) but when the app switch actually happens I receive a EXC_BAD_ACCESS instead of the didFinishSendingToApplication: delegate callback. Before I ported this code to Swift from Objective-C everything was working fine.
Any ideas whats is going wrong here?
self.documentInteractionController = UIDocumentInteractionController(URL: self.fileURL)
self.documentInteractionController!.UTI = "net.whatsapp.movie"
self.documentInteractionController!.delegate = self
Here is all of the stack trace I can get (the crash actually happens in the main function so I can't get anything more...):
* thread #1: tid = 0x12ef7e, 0x0000000182dd18c8 CoreFoundation`CFStringCreateCopy + 36, queue = 'com.apple.main-thread', stopreason = EXC_BAD_ACCESS (code=1, address=0x0)
* frame #0: 0x0000000182dd18c8 CoreFoundation`CFStringCreateCopy + 36
frame #1: 0x0000000101df09d8 libswiftCore.dylib`function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Dead> of Swift.String.init (Swift.String.Type)(_cocoaString : Swift.AnyObject) -> Swift.String + 108
frame #2: 0x0000000101dd45b0 libswiftCore.dylib`Swift.String.init (Swift.String.Type)(_cocoaString : Swift.AnyObject) -> Swift.String + 24
frame #3: 0x0000000100253814 MyApp`#objc MyApp.MyViewController.documentInteractionController (MyApp.MyViewController)(ObjectiveC.UIDocumentInteractionController, didEndSendingToApplication : Swift.String) -> () + 84 at MyViewController.swift:0
frame #4: 0x0000000183dedf9c Foundation`__NSThreadPerformPerform + 372
frame #5: 0x0000000182ea4240 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
frame #6: 0x0000000182ea34e4 CoreFoundation`__CFRunLoopDoSources0 + 264
frame #7: 0x0000000182ea1594 CoreFoundation`__CFRunLoopRun + 712
frame #8: 0x0000000182dcd2d4 CoreFoundation`CFRunLoopRunSpecific + 396
frame #9: 0x000000018c5e36fc GraphicsServices`GSEventRunModal + 168
frame #10: 0x0000000187992fac UIKit`UIApplicationMain + 1488
frame #11: 0x00000001000c6374 MyApp`main(argc=1, argv=0x000000016fdf79c0) + 124 at main.m:33
frame #12: 0x0000000194d8ea08 libdyld.dylib`start + 4
The real issue is that the delegate signature is wrong on the SDK. Instead of
documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String)
you should use
documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String?)
Please not the last ?, which says the application parameter can be nil, which it always is.
You should do the same to the willBeginSendingToApplication call, as it too can have a nil application parameter.
It's safe to ignore the warnings complaining about different optionality than expected by the protocol.
I met the same problem and spent days to try to fix this problem.
I finally found that the root cause of this error is from implementing func documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String) in the class.
I removed this function and use willBeginSendingToApplication instead and the app will not crash on app switching.
Tested on real device running iOS 7.1 and iOS 8.3. FYI

EXC_BAD_INSTRUCTION upon pressing a button

I'm newbie in Swift and I'm currenlty trying to learn it but I faced a mystery issue. As you can see in the image below, there are some controls that are linked properly to this View Controller and upon pressing a button in the UI we execute this code below but I always got an EXC_BAD_INSTRUCTION. I don't understand why!!
Here is some info from the stack trace.
(lldb) thread info
thread #1: tid = 0x9319, 0x0006c5d5 App`App.AViewController.sendCommentViaEmail (self=<unavailable>)() -> () + 9349 at AViewController.swift:49, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
(lldb) frame select 1
frame #1: 0x037067cd libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84
libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84:
-> 0x37067cd: addl $0x1c, %esp
0x37067d0: popl %esi
0x37067d1: popl %edi
0x37067d2: popl %ebx
EDITED:
Stacktrace below
* thread #1: tid = 0x1e19, 0x0005a5d5 A`A.AViewController.sendCommentViaEmail (self=<unavailable>)() -> () + 9349 at AViewController.swift:49, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
* frame #0: 0x0005a5d5 A`A.AViewController.sendCommentViaEmail (self=<unavailable>)() -> () + 9349 at AViewController.swift:49
frame #1: 0x036f47cd libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84
frame #2: 0x01ea323d UIKit`-[UIApplication sendAction:to:from:forEvent:] + 99
frame #3: 0x02213840 UIKit`-[UIBarButtonItem(UIInternal) _sendAction:withEvent:] + 139
frame #4: 0x036f47cd libobjc.A.dylib`-[NSObject performSelector:withObject:withObject:] + 84
frame #5: 0x01ea323d UIKit`-[UIApplication sendAction:to:from:forEvent:] + 99
frame #6: 0x01ea31cf UIKit`-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
frame #7: 0x01fd6e86 UIKit`-[UIControl sendAction:to:forEvent:] + 69
frame #8: 0x01fd72a3 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 598
frame #9: 0x01fd650d UIKit`-[UIControl touchesEnded:withEvent:] + 660
frame #10: 0x01ef360a UIKit`-[UIWindow _sendTouchesForEvent:] + 874
frame #11: 0x01ef40e5 UIKit`-[UIWindow sendEvent:] + 791
frame #12: 0x01eb9549 UIKit`-[UIApplication sendEvent:] + 242
frame #13: 0x01ec937e UIKit`_UIApplicationHandleEventFromQueueEvent + 20690
frame #14: 0x01e9db19 UIKit`_UIApplicationHandleEventQueue + 2206
frame #15: 0x039791df CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
frame #16: 0x0396eced CoreFoundation`__CFRunLoopDoSources0 + 253
frame #17: 0x0396e248 CoreFoundation`__CFRunLoopRun + 952
frame #18: 0x0396dbcb CoreFoundation`CFRunLoopRunSpecific + 443
frame #19: 0x0396d9fb CoreFoundation`CFRunLoopRunInMode + 123
frame #20: 0x085dc24f GraphicsServices`GSEventRunModal + 192
frame #21: 0x085dc08c GraphicsServices`GSEventRun + 104
frame #22: 0x01ea18b6 UIKit`UIApplicationMain + 1526
frame #23: 0x000919cc A`main(argc=1, argv=0xbffaa62c) + 76 at main.m:12
frame #24: 0x04490ac9 libdyld.dylib`start + 1
Here are the properties I'm using:
I got the same result whether they are linked to the UI or not.
Where does emailToMyselfSwitch come from? If it's an IBOutlet it seems that you forgot to connect it from IB to your code. If that's not the case please show more context.
This error EXC_BAD_INSTRUCTION generally means that you are calling a property or method (selector?) on a null object or an object that can't respond to the selector ie self.null.on or a property that cannot be accessed because it was released.
I've had trouble in the past with these weak vars falling away. Try deleting the weak in your property.
your code is correct. The problem is not in the connection with outlet. I´ve tested and prints: "Value: nil"
The problem is in other part of code.
The message "self=" is not normal.
You can show us more information.

-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke crash

I'm having an issue finding the source of this crash in my iOS app which has a core data model using iCloud to store user search history and user favourites. The app crashes after pressing the home button then restarting the app. I does not happen on the first use, but usually fourth or fifth time the app is started.
The backtrace is below:
* thread #1: tid = 0xea2b, 0x02e5c88a libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread, stop reason = breakpoint 1.1
frame #0: 0x02e5c88a libobjc.A.dylib`objc_exception_throw
frame #1: 0x03225903 CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:] + 275
frame #2: 0x0317890b CoreFoundation`___forwarding___ + 1019
frame #3: 0x031784ee CoreFoundation`_CF_forwarding_prep_0 + 14
frame #4: 0x011a3bf9 Foundation`__57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke + 40
frame #5: 0x031e4524 CoreFoundation`__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
frame #6: 0x0313c00b CoreFoundation`_CFXNotificationPost + 2859
frame #7: 0x010dd951 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 98
frame #8: 0x014dacb8 UIKit`-[UIApplication _deactivateForReason:notify:] + 370
frame #9: 0x014dada6 UIKit`-[UIApplication _deactivateForReason:] + 48
frame #10: 0x014e7e14 UIKit`-[UIApplication _handleApplicationSuspend:eventInfo:] + 354
frame #11: 0x014f4a45 UIKit`-[UIApplication handleEvent:withNewEvent:] + 3904
frame #12: 0x014f4de9 UIKit`-[UIApplication sendEvent:] + 85
frame #13: 0x014e2025 UIKit`_UIApplicationHandleEvent + 736
frame #14: 0x040a62f6 GraphicsServices`_PurpleEventCallback + 776
frame #15: 0x040a5e01 GraphicsServices`PurpleEventCallback + 46
frame #16: 0x03103d65 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
frame #17: 0x03103a9b CoreFoundation`__CFRunLoopDoSource1 + 523
frame #18: 0x0312e77c CoreFoundation`__CFRunLoopRun + 2156
frame #19: 0x0312dac3 CoreFoundation`CFRunLoopRunSpecific + 467
frame #20: 0x0312d8db CoreFoundation`CFRunLoopRunInMode + 123
frame #21: 0x040a49e2 GraphicsServices`GSEventRunModal + 192
frame #22: 0x040a4809 GraphicsServices`GSEventRun + 104
frame #23: 0x014e1d3b UIKit`UIApplicationMain + 1225
frame #24: 0x0000214d MBSSearch`main(argc=1, argv=0xbfffeed8) + 141 at main.m:15
I added a symbolic breakpoint -[NSNotificationCenter addObserver:selector:name:object:] with a debugger command PO but get a ton of useless iCloud data in the debug output like this:
2013-12-25 10:06:48.338 MyApp[1543:350f] -[PFUbiquityTransactionLog loadContents:](345): CoreData: Ubiquity: Error encountered while trying to load the comparison metadata for transaction log: <PFUbiquityTransactionLog: 0xf365da0>
transactionLogLocation: <PFUbiquityLocation: 0xb62e990>: /Users/me/Library/Application Support/iPhone Simulator/7.0.3-64/Library/Mobile Documents/WS9VY4R2C6~com~me~myapp/data/me~simF624C941-DBCF-57CE-98C0-0DC98418B1E4/Appstore/~qmbb44VcI_8d7OJMA6dsq8P47PO1E4pQCb7q5bp3JA=/5E401B20-4502-4E53-9B76-415122EE8882.1.cdt
transactionNumber: (null)
Error: Error Domain=NSCocoaErrorDomain Code=134302 "The operation couldn’t be completed. (Cocoa error 134302.)" UserInfo=0xb642ca0 {reason=Error reading the log file at location: (null)
userInfo: (null)}
userInfo: {
reason = "Error reading the log file at location: (null)\nuserInfo: (null)";
Any ideas on tracing the cause of the crash?
EDIT: I've also tried looking for NSZombies using Instruments and can reproduce the crash without a zombie error
EDIT2: This link is extremely helpful: http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1
What do you mean
I added a symbolic breakpoint -[NSNotificationCenter
addObserver:selector:name:object:] with a debugger command PO
You don't use the PO command to add a symbolic breakpoint. The easiest way to add a symbolic breakpoint is using the breakpoint navigator in Xcode. You could also look up the breakpoint commands in the LLDB command line, but those are a little harder to figure out.
As for your crash, it looks like you've set up a notification center notification for suspend events. Do you add any notification manager observers? Or are you using any third party libraries? You might have a third party library that is adding a notification that you are not aware of.

Resources