mikeash.com pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsmikeash.com Recent CommentsTue, 19 Mar 2024 07:35:00 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssJames Walker - 2015-04-16 17:08:52http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsI've found that setting the <code>__crashreporter_info__</code> string as described here doesn't work in stripped builds. The asm directive is allegedly supposed to fix that, but in my experience it doesn't. What does seem to work is to declare the string external, i.e., <code>extern const char* __crashreporter_info__;</code> instead of declaring your own global variable. Or you could get it dynamically as is done here: <a href="http://jens.ayton.se/blag/hackin-ur-crash-reportz/">http://jens.ayton.se/blag/hackin-ur-crash-reportz/</a>434fbb90de017646196b3babf7eda32dThu, 16 Apr 2015 17:08:52 GMTSayeed Munawar Hussain - 2014-10-31 06:45:12http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsAnother gem of a post. I turned a believer in the idea of never disabling asserts even in production builds after reading your reasoning. Thanks again for this one.4035a3c49bd12062641d63e81a6bf40bFri, 31 Oct 2014 06:45:12 GMTbryn austin bellomy - 2013-08-10 17:57:20http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsIn case it's any use to anyone, I maintain an open-source library (available over CocoaPods) with a bunch of commonplace assert macros specific to Objective-C. It integrates pretty seamlessly with CocoaLumberjack (for logging) and XcodeColors (for being able to READ the resulting logs, at least in the Xcode console). <br /> <br />The library is called BrynKit (it's basically a personal and informally-maintained thing, although if people like the logging and debugging tools, I might break them out into a separate library/pod). It's available here: <a href="http://github.com/brynbellomy/BrynKit">http://github.com/brynbellomy/BrynKit</a> <br /> <br />The assertion macros themselves are in <a href="https://github.com/brynbellomy/BrynKit/blob/master/Main/BrynKitDebugging.h">https://github.com/brynbellomy/BrynKit/blob/master/Main/BrynKitDebugging.h</a> <br /> <br />I'd love any feedback or contributions anyone might want to offer, as I'm sure the library could stand to mature quite a bit ;) Hope it's useful to someone out there.8e2c849e3a5817d8259fe65250cbe414Sat, 10 Aug 2013 17:57:20 GMTJames Walker - 2013-06-18 17:36:23http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#comments@Sandro Stricker: No, just put these lines in ONE source file, in every target where you use the macro. That's how you avoid a duplicate symbols error.352015051d3a1d3a7fc5cae65ada02a4Tue, 18 Jun 2013 17:36:23 GMTSandro Stricker - 2013-06-18 14:23:15http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#comments@James Walker: So I need to put this in every source file where I use this macro? That looks to me like a bad solution.dc74018731811e7f9612920dfed0b0d0Tue, 18 Jun 2013 14:23:15 GMTJames Walker - 2013-06-17 23:51:37http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#comments@Sandro Stricker: I'm pretty sure you must put them in a source file. <br /> <br />@me: That's mysterious, that asm line seems to work either way, with 3 underscores at the start and 2 at the end, or 1 at the start and 0 at the end.1026d72380d07f18ee8f927284335fdbMon, 17 Jun 2013 23:51:37 GMTSandro Stricker - 2013-06-17 15:35:50http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsWhere to put those two lines: <br /><code> <br />const char *__crashreporter_info__ = NULL; <br />asm(".desc _crashreporter_info, 0x10"); <br /></code> <br /> <br />I wanted to put them in a header, but that didn't work out (duplicate symbols error). Can I put them into the macro?afcb19bf699a776b67445d497fc3b9ccMon, 17 Jun 2013 15:35:50 GMTJames Walker - 2013-06-16 17:27:16http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsIn the asm line for crashreporter_info, have you lost some underscores to wiki formatting? Michael Tsai's version at <a href="http://mjtsai.com/blog/2013/02/27/application-specific-crash-report-information/">http://mjtsai.com/blog/2013/02/27/application-specific-crash-report-information/</a> has 3 at the start and 2 at the end.5f9c1c0212b5dcb8a77f4b21b5425859Sun, 16 Jun 2013 17:27:16 GMTSimon - 2013-06-03 08:40:44http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#comments<div class="blogcommentquote"><div class="blogcommentquoteinner">Typically, the most useful place for asserts is at the top of a function or method, to check constraints on the parameters that can't be expressed in the language directly.</div></div> <br /> <br />This is what I usually used NSException for. Check using an if statement, raise an NSException if true. Having read this post, asserts seem to be the better solution. While it is clear to me when to use NSError (thanks to <a href="http://www.jayway.com/2010/10/13/exceptions-and-errors-on-ios">http://www.jayway.com/2010/10/13/exceptions-and-errors-on-ios</a>, I'm left wondering when to use NSException? <br /> <br />Any good guideline when to use NSException? <br /> <br />Cheers <br />Simonb2166690991a08b3fb6964c6bbc76de3Mon, 03 Jun 2013 08:40:44 GMTJavier Soto - 2013-05-10 16:56:48http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsAnother good but frequently overlooked reason to use NSCParameterAssert is that NSParameterAssert references self inside of the macro, so if you use it inside of a block you might cause a retain cycle without even noticing it! <br />In order to remember, I like to think of it as if a block were a C function (not far from the truth) instead of an ObjC method.f19422aecf9ee1bbbcb84271237397aeFri, 10 May 2013 16:56:48 GMTPatrick Stein - 2013-05-06 08:33:29http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsA good thing to do is to change the way asserts are handled in production code. <br />In production code show up a warning to let the user know what has happened. <br />In debug builds just crash ;-) <br /> <br />Patrick aka Jolly6a6bd78def8e74fd35e4d8ed025f1f7fMon, 06 May 2013 08:33:29 GMTBenedict Cohen - 2013-05-04 08:10:49http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsI have 2 assert macros for handling threading. One asserts that we're on the main thread and the other asserts that we're not on the main thread. They're handy for blocks which can get passed from the controller to the model and contain UIKit objects which should only be main thread.00377112acbf8f6b91ba6b521667dd16Sat, 04 May 2013 08:10:49 GMTFernando - 2013-05-03 23:54:05http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsThe way I handle asserts is I disable them for production, but try to avoid a bad crash by handling them at a very basic level. For instance: <br /> <br /><code> <br />NSAssert(foo, @"foo wasn't supposed to be nil"); <br />if (!foo) { <br />&nbsp;&nbsp;&nbsp;&nbsp;return; // don't do anything <br />} <br /> <br />// do something <br /></code>16471972b6ac2b429e66ea56dc584339Fri, 03 May 2013 23:54:05 GMTbob - 2013-05-03 20:38:11http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#comments@U.Hertlein: Yes, that's a good trick for adding a custom message, and I use it sometimes. Unfortunately, the message must be fixed in the source code at compile-time. It is impossible to include runtime information with the C assert macro.89ea3f405348dc92cda6a3ecbb1bd403Fri, 03 May 2013 20:38:11 GMTU.Hertlein - 2013-05-03 14:21:03http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsI always use <br /> <br /><code>assert(ptr != NULL &amp;&amp; "Invalid pointer");</code> <br /> <br />to provide a custom message with the assert failure.3a148969930e2b706d5a48d513c9ceb3Fri, 03 May 2013 14:21:03 GMTMatthijs Hollemans - 2013-05-03 13:39:38http://www.mikeash.com/?page=pyblog/friday-qa-2013-05-03-proper-use-of-asserts.html#commentsAsserts are also handy for making the static analyzer shut up about certain warnings. For example, if the analyzer complains that you're using a pointer that may be NULL and you're sure it will never be NULL, add an assert() statement to prove that to the compiler. <br />b2df0dcb27ab918ed447720253348e2fFri, 03 May 2013 13:39:38 GMT