Skip to main content

iPhone - How to test allocated objects returned by each memory call?



When writing an app, you always have to write alloc/inits, get autoreleased datas returned by the framework classes, ...





This may be 70% of the code, almost each single line of what you write...





So... How do those returned object must be tested, to know if each of these calls have returned a correct object ?





Testing the returned value each time, for each call, and treating the exception if you get nil where you expected an allocated object ? Letting the app crash ?





How this must be done ?


Comments

  1. If it's fatal, just abort -- Consider a custom error handler for this case so the info can help you diagnose the issue.

    If it's not fatal and you can continue, do so. "Oops, that string could not be converted to some encoding, but that's not a show stopper".

    If you can and should present a message instead, do so. Restart may be a safer time to present a message in some cases.

    Exceptions in Cocoa -- catch and handle what you must, but Cocoa exceptions are generally "non-recoverable". C++ exceptions are fine, but you may not get too far using them if you are working with ObjC and C++. The problem in its simplest form is that exceptions are not guaranteed to safely cross image boundaries -- ObjC exceptions are C++ exceptions (in OS X 64 bit and iOS).

    Error parameters are another way to go. I'm sure you have seen: :(NSError**)outError

    The important thing is that you understand the consequences of each approach and write appropriately.

    ReplyDelete
  2. It isn't necessary to check for a nil return value every time you call a function that returns an object. The important thing is to have some sense of when a function is more likely to return a nil object and what the consequences might be for your app.

    Functions related to the file system or network are two of the most common types that you will often need to check for nil return values, because the state of those two systems is generally beyond your control, so the resources you are trying to access may be unavailable. A good indication that you should be checking for a nil return value is if the function also takes an NSError ** as an argument.

    On the other hand, checking for nil every time you create an NSString or NSArray, for example, is unnecessary because the only reason it would ever return nil is if there has been some kind of programmer error that you should catch during testing anyway.

    So I guess my very general advice is not to check for nil when the only practical way it could be nil is if there was a programmer error, but do check for nil when the return object is something that could be nil as a result of some resource not being available (and out of your control).

    Also keep in mind that messages to nil are OK in Objective-C, which also eliminates the need for a lot of nil testing in certain cases.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex