Wednesday, January 2, 2008

Logging Messages to Nil

Bill Bumgarner shows how to use the private _objc_setNilReceiver() function and -resolveInstanceMethod: to log the first time each selector is sent to nil. I imagine that by uncommenting the print statement in -forwardInvocation: this could be made to work with Objective-C 1.x, but you’d get a log entry for every message sent to nil.

Update: Bumgarner shows how to do the logging using Leopard’s DTrace.

Update 2: How to do it with Instruments.

1 Comment RSS · Twitter


It should "just work" against Tiger as the code was developed based on the information found in the darwin source's for Objective-C.

And, yes, you'll get lots and lots and lots of log output. Turns out that a surprising number of classes rely upon nil-eats-message!

However, it'd be easy to fix by adding a simple hash to the -forwardInvocation: to make it log once per selector. Just remember to do so in a thread safe fashion!

Leave a Comment