{"id":129,"date":"2006-05-05T08:16:58","date_gmt":"2006-05-05T15:16:58","guid":{"rendered":"http:\/\/www.red-sweater.com\/blog\/129\/coregraphics-log-jam"},"modified":"2006-05-05T08:21:27","modified_gmt":"2006-05-05T15:21:27","slug":"coregraphics-log-jam","status":"publish","type":"post","link":"https:\/\/redsweater.com\/blog\/129\/coregraphics-log-jam","title":{"rendered":"CoreGraphics Log Jam"},"content":{"rendered":"<p>It&#8217;s been a while since I&#8217;ve complained about Apple in the context of an actual bug report, so here is a little diversion back into &#8220;Apple Bug Friday&#8221; land.<\/p>\n<p>\nOne of the aspects of Mac OS X programming that can really frustrate traditional Mac OS programmers is the lack of error reporting from many APIs. While traditional Carbon APIs tend to return numeric failure codes when something goes wrong, it&#8217;s not unusual for CoreFoundation and related APIs to simply return NULL. What&#8217;s the developer to do in this case? The usual argument is that NULL is returned only from functions where a more specific error would not help the programmer convey meaningful information to the user. In other words, if it returns NULL when you don&#8217;t expect it to, your computer is either melting down, or you&#8217;ve got some debugging to do. Regardless of the validity of this argument, it can be infuriating to look at code that seems perfectly correct, and be clueless as to why it is failing.\n<\/p>\n<p>\nI was chatting with <a href=\"http:\/\/www.toxicsoftware.com\/\">Jonathan Wight<\/a> when he shared his frustration with the CGBitmapContextCreate function. He was in the midst of debugging just one of these mind-boggling NULL returns. All the parameters looked <em>more or less<\/em> correct but the function just plain refused to cooperate.\n<\/p>\n<p>\nI guess I was between builds or something because I got interested in the problem. While Jon took the more efficient path of carefully examining the parameters being passed and comparing their values to the documentation&#8217;s restrictions, I started traipsing through assembly in Xcode. You know, the fun stuff.\n<\/p>\n<p>\nOf course, Jon found the answer first. It was a problem with the combination of colorspace and alpha parameters. The limitations are well-documented (if a bit overwhelming) in Apple&#8217;s <a href=\"http:\/\/developer.apple.com\/qa\/qa2001\/qa1037.html\">Technical Q&#038;A<\/a>. In this case, Jon was fetching the values directly from a CGImage that had been loaded from disk. Can&#8217;t blame a guy for thinking that maybe a JPG image&#8217;s attributes might be suitable for creating a bitmap context!\n<\/p>\n<p>\nEven after the bug was resolved, I continued digging into the framework via the debugger. I&#8217;m due for a complete retraining on Intel, but as long as I have my PowerPC machine around, I&#8217;m fairly comfortable zipping through the various API to see what <em>really<\/em> happens when an error occurs.\n<\/p>\n<p>\nSure enough, a few levels into the API, I spot an examination of the input colorspace, and an objection to the fact that it was only 24-bits per pixel. Great, I thought. If the API knows exactly what&#8217;s wrong, why doesn&#8217;t it let us know <em>somehow<\/em>, at least through a logging message or something. Then I spotted a  call to &#8220;CGPostError.&#8221; Well, that&#8217;s interesting. A well-defined error reporting mechanism? The results of which the developer never sees? I traced along a while until discovered myself in a logging function, the parameter to which was this choice bit of text:\n<\/p>\n<p><pre>\nCGBitmapContextCreate: unsupported parameter combination: 8 integer bits\/component;\n\t24 bits\/pixel; 3-component colorspace; kCGImageAlphaNone.\n<\/pre>\n<\/p>\n<p>\nWell! It doesn&#8217;t exactly point to the right solution, but it would have been nice to know! Thanks for not sharing, CoreGraphics. Why the heck does it go to all this work and not print out the result? I decided to dig deeper.\n<\/p>\n<p>\nThe framework has an internal function, &#8220;hasTerminal,&#8221; which seems to control whether logging to the console occurs or not. I guess the engineers on the CoreGraphics team are trying to be considerate and not spew logging messages all over your console log unless you&#8217;re actually, you know, debugging. Here&#8217;s the kicker &#8211; hasTerminal returns true if you are running from the terminal. All Jon had to do was test his app from the command-line and the exceedingly helpful logging message would have appeared.\n<\/p>\n<p>\nBut the &#8220;hasTerminal&#8221; function returns false when debugging from within Xcode. Bugger that debugger! How does hasTerminal decide whether to allow logging or not? It simply attempts to open &#8220;\/dev\/tty&#8221; with read\/write access. If it is successful, the user must be running from a terminal?  But somehow, when running from within Xcode, this test fails, and therefore logging is not done. I assume this has something to do with Xcode&#8217;s magic terminal-esque window, but the discovery raises two questions:\n<\/p>\n<ol>\n<li>Should CoreGraphics limit its logging functionality based on assumptions about &#8220;\/dev\/tty&#8221;?<\/li>\n<li>Should debugging in Xcode impact a target application&#8217;s ability to open &#8220;\/dev\/tty&#8221;?<\/li>\n<\/ol>\n<p>\nThese questions are posed in Radar #<a href=\"rdar:\/\/problem\/4538344\">4538344<\/a>&#8211; &#8220;CoreGraphics logging doesn&#8217;t appear withing Xcode&#8221;.\n<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a while since I&#8217;ve complained about Apple in the context of an actual bug report, so here is a little diversion back into &#8220;Apple Bug Friday&#8221; land. One of the aspects of Mac OS X programming that can really frustrate traditional Mac OS programmers is the lack of error reporting from many APIs. [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,11,2,15,5],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-applebugfriday","category-cocoa","category-debugging","category-programming","category-xcode"],"_links":{"self":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":0,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redsweater.com\/blog\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}