{"id":54,"date":"2008-05-31T17:14:47","date_gmt":"2008-05-31T22:14:47","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/05\/31\/messages-to-nowhere\/"},"modified":"2009-04-19T09:51:21","modified_gmt":"2009-04-19T14:51:21","slug":"messages-to-nowhere","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/05\/31\/messages-to-nowhere\/","title":{"rendered":"Messages to Nowhere"},"content":{"rendered":"<p>If you send a message to (call a method of) an object that is <code>nil<\/code> (<code>NULL<\/code>) in Objective-C, nothing happens, and the result of the message is <code>nil<\/code> (aka <code>0<\/code>, aka <code>0.0<\/code>, aka <code>NO<\/code> aka <code>false<\/code>).  At least most of the time. There is an important exception if <code>sizeof(return_type) &gt; sizeof(void*)<\/code>; then the return-value is <b>undefined<\/b> under PowerPC\/iPhone, and thus all Macs for the next several years.  So watch out if you are using a return value that is a <code> struct, double, long double,<\/code> or <code>long long<\/code>.<\/p>\n<p><a href=\"http:\/\/developer.apple.com\/documentation\/MacOSX\/Conceptual\/universal_binary\/universal_binary_tips\/chapter_5_section_22.html\">The fully story:<\/a><\/p>\n<blockquote><p>\nIn Objective-C, it is valid to send a message to a <code>nil<\/code> object. The Objective-C runtime assumes that the return value of a message sent to a <code>nil<\/code> object is <code>nil<\/code>, as long as the message returns an object or any integer scalar of size less than or equal to <code>sizeof(void*).<\/code><br \/>\nOn Intel-based Macintosh computers, messages to a <code>nil<\/code> object always return <code>0.0<\/code> for methods whose return type is <code> float, double, long double, <\/code> or <code>long long.<\/code> Methods whose return value is a <code>struct<\/code>, as defined by the <a href=\"http:\/\/developer.apple.com\/documentation\/DeveloperTools\/Conceptual\/LowLevelABI\/index.html#\/\/apple_ref\/doc\/uid\/TP40002521\"> Mac OS X ABI Function Call Guide<\/a> to be returned in registers, will return <code>0.0<\/code> for every field in the data structure. Other <code>struct<\/code> data types will not be filled with zeros. This is also true under Rosetta. On PowerPC Macintosh computers, the behavior is undefined.<\/p><\/blockquote>\n<p>I was recently bitten by this exceptional behavior.  I was using an <code>NSRange<\/code> <code>struct<\/code> describing a substring;  but the string was <code>nil<\/code>, so the substring was garbage.  But only on a PPC machine!  Even running under Rosetta wouldn&#8217;t have reproduced the bug on my MacBook Pro.Undefined values can be a hard bug to detect, because they may be reasonable values when tests are run.<\/p>\n<p>Code running in the iPhone simulator will return all-zero stucts when messaging <code>nil<\/code>, but the same code running on an actual device will return undefined structs. Be aware that <strong>testing in the simulator isn&#8217;t enough to catch these bugs<\/strong>.<\/p>\n<p>A few simple guidelines can help you avoid my misfortune:<\/p>\n<ul>\n<li> Be especially careful using of any objective-C method that returns a <code>double, struct,<\/code> or <code>long long<\/code>\n<li> Don&#8217;t write methods that return a <code>double<\/code>, <code>struct<\/code>, or<code>long long<\/code>.  Return an object instead of a <code>struct<\/code>; an <a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/Foundation\/Classes\/NSNumber_Class\/Reference\/Reference.html\"><code>NSNumber*<\/code><\/a> or <code>float<\/code> instead of a <code>double<\/code> or <code>long long<\/code>.  If you must return a dangerous data type, then see if you can avoid it. There really isn&#8217;t a good reason to return a <code>struct<\/code>, except for efficiency. And when micro-optimizations like that matter, it makes more sense to write that procedure in straight-C, which avoids the overhead of Objective-C message-passing, and solves the undefined-return-value problem.\n<li> But if you absolutely must return a dangerous data type, then return it in a parameter.<br \/>\nBad:<br \/>\n<code>- (double) crazyMath;<\/code><br \/>\nGood:<br \/>\n<code>- (void) crazyMathResult:(double*)result;<\/code>.\n<\/ul>\n<p>I love Objective-C&#8217;s &#8220;nil messaging&#8221; behavior, even though it is rough around the edges.  It&#8217;s usefulness is beyond the scope of this article, but it can simplify your code if you don&#8217;t return a data-type that is larger then <code>sizeof(void*)<\/code>.  With time, when the intel-style return behavior can be universally relied on, things will be even better.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you send a message to (call a method of) an object that is nil (NULL) in Objective-C, nothing happens, and the result of the message is nil (aka 0, aka 0.0, aka NO aka false). At least most of the time. There is an important exception if sizeof(return_type) &gt; sizeof(void*); then the return-value is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,6,203,3,5,4],"tags":[106,388,78,275],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-bug-bite","category-cocoa","category-iphone","category-macosx","category-objective-c","category-programming","tag-nil","tag-nsrange","tag-nsstring","tag-objective-c-10"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}