Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread Display Modes
Old Mar 23, 2006, 04:16 AM   #1
macrumors 601
gekko513's Avatar
Join Date: Oct 2003
Finding the source of Cocoa/ObjC exceptions

Is there an easy way of finding out where a Cocoa exception, such as "*** -[NSCFDictionary setObject:forKey:]: attempt to insert nil value", originates from?

By where I mean which method in my own code made the offending call to NSCFDictionary. I'm not even sure if this is really an exception or if it's just an error log.

Is it possible to make a conditional breakpoint in methods that are in the imported frameworks?

Or can I create a proxy method to catch calls and then forward them, so that I can put up a breakpoint in the proxy method instead?
gekko513 is offline   0 Reply With Quote
Old Mar 23, 2006, 05:17 AM   #2
caveman_uk's Avatar
Join Date: Feb 2003
Location: Hitchin, Herts, UK
I generally end up putting a load of NSLog()s and NSAsserts in to find out. I guess there must be a better way.
caveman_uk is offline   0 Reply With Quote
Old Mar 24, 2006, 01:01 AM   #3
Thread Starter
macrumors 601
gekko513's Avatar
Join Date: Oct 2003
For those interested in an update on this: I found the offending method call, but not as easily as I would like.

I did, however, discover the "poseAs" feature in Objective C, a useful debugging trick in general although it didn't work as elegantly as I had hoped in this case.

PoseAs lets a subclass pose as it's super-class. This can among other things be used to add logging messages or breakpoints in classes that you haven't implemented yourself.

I first wanted to do that for NSCFDictionary, but I couldn't find a .h file with the interface for it, so I had to settle for NSMutableDictionary, which I assume use the NSCFDictionary and causes the error. I tried this and I was able to log calls to setObject:forKey:, but I still wasn't able to find the error.

Then I decided to try from another angle. I knew the error occured when makeKeyAndOrderFront: was called on my Preferences windows. I didn't know how to put a breakpoint there and step through to see exactly where the error occured, but this problem could easily be solved by "poseAs".

In SomeClassThatIUseInTheNib.m I added:
@interface ZWindow : NSWindow {
@implementation ZWindow
- (void)makeKeyAndOrderFront:(id)sender {
	NSLog(@"%s", __PRETTY_FUNCTION__);
	[super makeKeyAndOrderFront:sender];

@implementation SomeClassThatIUseInTheNib
+ (void) initialize {
	[ZWindow poseAsClass:[NSWindow class]];
This let me put a breakpoint on makeKeyAndOrderFront: and I was able to step through and eventually find the cause for the error.

I would still be happy if anyone could tell me an easier way of doing this.
gekko513 is offline   0 Reply With Quote
Old Mar 24, 2006, 02:52 AM   #4
Thread Starter
macrumors 601
gekko513's Avatar
Join Date: Oct 2003
OK, even though I found the bug I wasn't quite happy with not having a decent way of tracking down a similar bug in the future so I investigated further and I found a better way.

First: Add a symbolic breakpoint (in the breakpoints window in the debugger) at -[NSCFDictionary setObject:forKey:]

This isn't very helpful in itself because that method is called millions of times, but you get to see what happens in that method (sort of, most of it is in assembly), and I figured that the function call _NSDictionaryRaiseInsertNilValueException was the one I really wanted to break at:

Second: Add a symbolic breakpoint at _NSDictionaryRaiseInsertNilValueException, then disable the first breakpoint.

That's it.
gekko513 is offline   0 Reply With Quote

MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
How I learned mouse drivers are the source of all the worlds evil Klungo Mac Basics and Help 7 Jul 29, 2009 04:25 PM
Help Me Diagnose The Source of this Kernel Panic NickD iMac 5 Mar 16, 2009 08:56 AM
What is the source of the "Newest iPhone Apps" section of the macrumors iphone tab? diesel iPhone and iPod touch Apps 5 Sep 30, 2008 11:33 AM
Find the length of a side of a triangle beatsme Community Discussion 8 Nov 25, 2006 05:25 PM

All times are GMT -5. The time now is 04:30 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013,, LLC