I’m aware that it’s perfectly fine to send messages to nil objects in Objective-C. However, I am curious if there is any runtime support for flagging such situations. I can see this being useful in testing/debugging situations.
I’m aware that it’s perfectly fine to send messages to nil objects in Objective-C.
Share
After a little fiddling with the debugger this is what I found out.
You can set a breakpoint in
objc_msgSendwith a breakpoint condition on the first argument to be zero (the receiver):objc_msgSend*(int*)($esp+4) == 0When you run your executable it will break very often since it’s very common to send messages to nil. To get an overview of what’s happening you can further configure your breakpoint:
p (char*)*(int*)($esp+8)in the command fieldWhen you now continue execution, you will see all the message names (being sent to nil) in the debugger console.
All the above is working on Intel Macs only (32 bit Cocoa or Cocoa Touch in the simulator). PPC or ARM architectures use other register names and calling conventions. I leave it as an exercise to you to find out how to get this working on these platforms 😉