Earlier this month when Safari Books Online was hosting its Cocoa Programming webcast with David Chisnall I searched Twitter for #Cocoa to see if anyone was talking about the webcast. I found someone asking about debugging an EXC_BAD_ACCESS deep down in objc_msgSend within the RunLoop. David Chisnall, author of the following books currently in the Safari Books Online library The Definitive Guide to the Xen Hypervisor and Cocoa® Programming Developer’s Handbook, Second Edition, kindly offered some suggestions for the following question.
How do you debug an EXC_BAD_ACCESS deep down in objc_msgSend within the RunLoop?
EXEC_BAD_ACCESS is what OS X reports when you try to access memory that is not valid, what most other systems call a segmentation violation, or simply SegV. If it happens in objc_msgSend() then it means that you are sending a message to an object that has been deallocated. If it happens in a run loop then you probably created an autoreleased instance of the object, which is automatically destroyed at the end of a run loop iteration. If you do something like this, for example:
array = [NSMutableArray array];
If array is a variable that persists beyond the current run loop iteration (an instance variable or a global) then you will see this if you send a message to array after the end of the run loop. All constructors other than +allocWithZone: (called by +alloc and +new) return autoreleased instances. Alternatively, you may be creating an object with +alloc -init and then sending it an -autorelease message.
The easiest way of debugging this is to set the NSZombieEnable environment variable. This will turn objects into instances of NSZombie rather than destroying them. You can then see what the object is. If you also enable malloc logging you can use the malloc_history command to find out where it was created. Check the Cocoa documentation for how to use these.
View the Webcast Recording
If you missed the Cocoa Programming webcast with David Chisnall, you can watch the recorded version for free. You can also watch Safari Books Online’s webcast recordings on topics like Joomla! Cocoa Programming, Presentation skills and you can read some interesting white papers on topics like Growing Your GreenIT Strategy. View the Downloads and Resources section of the corporate Website for more webcast and white paper information.
About David Chisnall
David Chisnall arrived at the Swansea University in 2000, looked at the sun and sea, and decided to stay there. Three years and one degree later, he was no longer under the illusion that the sun was a regular feature, but was persuaded to remain for another degree by the promise of a desk with a view of the sea. During his time as a PhD student, he worked hard at the best known of postgraduate activities: procrastination. This involved writing portions of A Practical Guide to RedHat Linux, Second Edition and regular articles for InformIT and a local tech news startup (which, as these are prone to do, has since gone bust). He is a founding member and core developer of the Étoilé project, which aims to build an open source user environment based for desktop and mobile computing systems. In spite of his best efforts, he managed to do some research in the areas of out of core pre-fetching algorithms and system simulation, and it wasn’t long before his supervisor started reminding him that a thesis was a requirement of a PhD. Knowing that a large task required an even larger distraction, he also agreed to write a book on the workings of Xen. This appeared to work, and he was able to complete his thesis within a fortnight of sending the complete draft to the publisher. He has since re-discovered sleep, and taken up dancing salsa while enjoying the (temporary) lack of deadlines.
On February 1, 2010, Mike Riley posted a highly favorable review of Cocoa Programming Developer’s Handbook on Dr Dobb’s CodeTalk. Feel free to read the review for yourself. We’d like to leave you with Mike’s closing paragraph:
“Overall, anyone committing themselves to developing for the Apple platform and already comfortable with Objective-C will find David Chisnall’s book an excellent reference for all things Cocoa. The author’s breadth and depth of knowledge are astounding, and the fact that he shares this education so thoroughly and effectively are testaments to his developer and communicator abilities. Every OSX developer should have this book on their desk.”