Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objc_msgSend() and EXC_BAD_ACCESS in cocoa app

Generally speaking, how do we avoid objc_msgSend() crashes in our Mac Apps? Also, how do we avoid the EXC_BAD_ACCESS crashes? Why do we get it? How can we manage these kinds of errors? They are on the increase on our app and would like to ask guidance and help on this matter.

We are using XCode 3.2.5 and our project is a Cocoa one, using Objective-C language. Our webservice is an IIS-powered ASP.Net website (from where we can download/upload an XML files and images).

We are developing a cocoa-app in Mac, and we encounter this like two times in ten tries of launching the app. This app has a sketch app, and when we run the app, it just freezes and crashes within 1 minute without doing anything. Some functions this app does is:

  • download xml from web and interpret on our app
  • login to a web service
  • has a sketch pad
  • has a timer
  • drag and drop functions
  • upload xml data
  • download images and display on our app

The crash is not limited to the post-launch of the app. Sometimes, when manipulating the program like drag-drop, downloading dataset, and doing add/edit/delete functions on our mac app, the app crashes as well.

Honestly speaking, we are relatively new to this development platform and environment, and we are still learning. Can you guide us on this issue? Like some resources or hints on where we did it wrong? Thanks a lot and more power to everyone here.


The folowing is an example of a crash report:

Process:         Sketch [63065]
Path:            /Users/william_hooley_27Inch/Desktop/untitled folder/MacGlass.app/Contents/MacOS/Sketch
Identifier:      com.apple.CocoaExamples.Sketch
Version:         1.049 (48.1)
Code Type:       X86 (Native)
Parent Process:  launchd [116]
Date/Time:       2011-04-14 13:12:15.421 +1000
OS Version:      Mac OS X 10.6.5 (10H574)
Report Version:  6
Interval Since Last Report:          714907 sec
Crashes Since Last Report:           17
Per-App Interval Since Last Report:  206437 sec

Per-App Crashes Since Last Report:   5
Anonymous UUID:                     
086C860F-E28E-4256-84F9-9692782AAD01
Exception Type:  EXC_BAD_ACCESS
(SIGBUS) Exception Codes:
KERN_PROTECTION_FAILURE at
0x0000000000000044 Crashed Thread:  0 
Dispatch queue: com.apple.main-thread
Thread 0 Crashed:  Dispatch queue:
com.apple.main-thread 0  
com.apple.AppKit             
0x91a8e6cb
_recursiveInvalidateCachedVisibleRectValue
+ 24 1   com.apple.CoreFoundation      0x995f02c0 CFArrayApplyFunction + 224
2   com.apple.AppKit             
0x91a8e70d
_recursiveInvalidateCachedVisibleRectValue
+ 90 3   com.apple.CoreFoundation      0x995f02c0 CFArrayApplyFunction + 224
4   com.apple.AppKit             
0x91a8e70d
_recursiveInvalidateCachedVisibleRectValue
+ 90 5   com.apple.CoreFoundation      0x995f02c0 CFArrayApplyFunction + 224
6   com.apple.AppKit             
0x91a8e70d
_recursiveInvalidateCachedVisibleRectValue
+ 90 7   com.apple.CoreFoundation      0x995f02c0 CFArrayApplyFunction + 224
8   com.apple.AppKit             
0x91a8e70d
_recursiveInvalidateCachedVisibleRectValue
+ 90 9   com.apple.CoreFoundation      0x995f02c0 CFArrayApplyFunction + 224
10  com.apple.AppKit             
0x91a8e70d
_recursiveInvalidateCachedVisibleRectValue
+ 90 11  com.apple.CoreFoundation      0x995f02c0 CFArrayApplyFunction + 224
12  com.apple.AppKit             
0x91a8e70d
_recursiveInvalidateCachedVisibleRectValue
+ 90 13  com.apple.CoreFoundation      0x995f02c0 CFArrayApplyFunction + 224
14  com.apple.AppKit             
0x91a8e70d
_recursiveInvalidateCachedVisibleRectValue
+ 90 15  com.apple.AppKit              0x91a8e2e1 -[NSView _setSuperview:] +
684 16  com.apple.AppKit             
0x91a8db02 -[NSView addSubview:] + 383
17  com.apple.CocoaExamples.Sketch
0x00094cbf -[viewTasksDisplay Appear:]
+ 242 18  com.apple.CocoaExamples.Sketch
0x000c2e8f -[MainWindow
ShowLeftScreen:] + 964 19 
com.apple.CocoaExamples.Sketch
0x0003e301 -[viewOptionMenuItemmouseDown:] + 2055 20 
com.apple.AppKit             
0x91bc6c68 -[NSWindow sendEvent:] +
5549 21  com.apple.AppKit             
0x91adf817 -[NSApplication sendEvent:]
+ 6431 22  com.apple.AppKit              0x91a732a7 -[NSApplication run] + 917
23  com.apple.AppKit             
0x91a6b2d9 NSApplicationMain + 574 24 
com.apple.CocoaExamples.Sketch
0x0001b33e main + 30 25 
com.apple.CocoaExamples.Sketch
0x00001eba start + 54 Thread 1: 
Dispatch queue:
com.apple.libdispatch-manager 0  
libSystem.B.dylib            
0x98002982 kevent + 10 1  
libSystem.B.dylib            
0x9800309c _dispatch_mgr_invoke + 215
2   libSystem.B.dylib            
0x98002559 _dispatch_queue_invoke +
163 3   libSystem.B.dylib            
0x980022fe _dispatch_worker_thread2 +
240 4   libSystem.B.dylib            
0x98001d81 _pthread_wqthread + 390 5  
libSystem.B.dylib            
0x98001bc6 start_wqthread + 30 Thread
2: 0   libSystem.B.dylib            
0x97fdc0fa mach_msg_trap + 10 1  
libSystem.B.dylib            
0x97fdc867 mach_msg + 68 2  
com.apple.CoreFoundation     
0x995f837f __CFRunLoopRun + 2079

Another crash report:

Date/Time:       2011-04-14 13:27:02
+1000 OS Version:      10.6.5 (Build 10H574) Architecture:    x86_64 Report
Version:  6 Command:         MacGlass
Path:           
/Users/william_hooley_27Inch/Desktop/untitled
folder/MacGlass.app/Contents/MacOS/Sketch
Version:         1.050 (48.1) Parent: 
launchd [116] PID:             63101
Event:           hang Duration:       
3.58s (sampling started after 2 seconds) Steps:           16 (100ms
sampling interval) Pageins:         0
Pageouts:        0 Process:        
Sketch [63101] Path:           
/Users/william_hooley_27Inch/Desktop/untitled
folder/MacGlass.app/Contents/MacOS/Sketch
UID:             501   Thread edef3d4 
DispatchQueue 100   User stack:
    16 start + 54 (in Sketch) [0x1f6a]
      16 main + 30 (in Sketch) [0x1b3ee]
        16 NSApplicationMain + 574 (in AppKit) [0x91a6b2d9]
          16 -[NSApplication run] + 821 (in AppKit) [0x91a73247]
            16 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:]
+ 156 (in AppKit) [0x91ab0fce]
              16 _DPSNextEvent + 847 (in AppKit) [0x91ab178d]
                16 BlockUntilNextEventMatchingListInMode
+ 81 (in HIToolbox) [0x95bd0bd6]
                  16 ReceiveNextEventCommon + 354 (in
HIToolbox) [0x95bd0d51]
                    16 RunCurrentEventLoopInMode + 392 (in
HIToolbox) [0x95bd0f9c]
                      16 CFRunLoopRunInMode + 97 (in
CoreFoundation) [0x995f7291]
                        16 CFRunLoopRunSpecific + 452 (in
CoreFoundation) [0x995f7464]
                          16 __CFRunLoopRun + 8059 (in CoreFoundation) [0x995f9adb]
                            16 __NSFireTimer + 141 (in Foundation) [0x972c39b0]
                              16 _handleWindowNeedsDisplay + 696 (in AppKit) [0x91adb28a]
                                16 -[NSWindow displayIfNeeded] + 204 (in AppKit) [0x91aa9d40]
                                  16 -[NSView displayIfNeeded] + 818 (in AppKit) [0x91ae0a57]
                                    16 -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:]
+ 4122 (in AppKit) [0x91b801cf]
                                      16 CAViewEndDraw + 134 (in QuartzCore)
[0x961bd926]
                                        16 pthread_cond_wait$UNIX2003 + 73 (in
libSystem.B.dylib) [0x9800b9f8]
                                          16 __semwait_signal + 10 (in
libSystem.B.dylib) [0x9800a0a6]  
Kernel stack:
    15 semaphore_wait_continue + 0 [0x22a3dd]
    1 lo_alltraps + 454 [0x2a08a6]
      1 i386_astintr + 47 [0x2a9a9a]
        1 ast_taken + 247 [0x219107]
          1 bsd_ast + 806 [0x489088]
            1 postsig + 432 [0x4861df]
              1 exit1 + 449 [0x47ab28]
                1 task_terminate_internal + 315
[0x22c56a]
like image 252
yokik88 Avatar asked Apr 14 '11 07:04

yokik88


4 Answers

The usual cause of something like this is over releasing objects. If you run your code with NSZombieEnabled, the console output should give you a clue as to where to look.

Alternatively, if this is a Macintosh application targeted at 10.5 or greater, you can enable garbage collection and not worry about retain and release at all.

like image 168
JeremyP Avatar answered Oct 25 '22 11:10

JeremyP


When this error occurs on an iOS app on a device connected with the debugger you can find out the selector being called by typing on the debugging console

(gdb) x/s $r1

This helped me find a problem when the accelerometer had its delegate still set to a deallocated object. Maybe this info is helpful for others.

like image 42
Pegolon Avatar answered Oct 25 '22 11:10

Pegolon


The second crash report is not a crash, that's a hang report. That means your app was slow, not crashing.

In general though, read and fully understand the Cocoa Memory Management guidelines; mistakes in memory management account for the vast majority of crashes in Cocoa apps. The guide can be found here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

You should also be able to use tools like the Xcode debugger, and the Zombies tool in Instruments to gather more detailed information about crashes.

like image 2
Catfish_Man Avatar answered Oct 25 '22 10:10

Catfish_Man


I was able to find the problematic selector with lldb, but the approach was different for me than described by the answer above using gdb semantics and an earlier version of iOS (I'm currently working with iOS 8)

My stack trace started like this:

libobjc.A.dylib`objc_msgSend:
0x10c160000:  testq  %rdi, %rdi
0x10c160003:  jle    0x10c160068        ; objc_msgSend + 104
0x10c160005:  movq   (%rdi), %r11
0x10c160008:  movq   %rsi, %r10
0x10c16000b:  andl   0x18(%r11), %r10d  <== EXC_BAD_ACCESS

At the lldb prompt, I did the following:

(lldb) register read -G s r10
     r10 = "\xffffffc16\xffffffa4\t\x01"  "isKindOfClass:"
like image 1
clearlight Avatar answered Oct 25 '22 11:10

clearlight