开发者

Can I get warnings for sending nil objects messages?

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 si开发者_Go百科tuations. I can see this being useful in testing/debugging situations.


After a little fiddling with the debugger this is what I found out.

You can set a breakpoint in objc_msgSend with a breakpoint condition on the first argument to be zero (the receiver):

  1. Open the "Breakpoints" window: Command-Option-B
  2. Double click on the last row to create a new symbolic breakpoint
  3. Enter the symbol: objc_msgSend
  4. Enter the condition in the "Condition" column: *(int*)($esp+4) == 0

When 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:

  1. Press the plus button to add a breakpoint command.
  2. Select "Debugger Command" from the popup.
  3. Enter p (char*)*(int*)($esp+8) in the command field
  4. Click the "auto-continue" checkbox in the breakpoint row (the one with the little arrow in the last column)

When 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 ;)


Alternatively, you can use dtrace:

Using dtrace to log traces messages-to-nil

And this can be done in Instruments, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜