Sunday, January 12, 2014

NSInvalidArgumentException that propagated all the way to my main class.

If you are here, you  probably got a NSInvalidArgumentException. Below is what I got, and even if your one is not the same, hopefully the explanation will help you solve yours:



Here is why this came all the way up:
- My application uses a documentInteractionController

  • It resides in a class
  • Helps to figure out 3rd party applications on my iOS device I can use to open certain files from my application
  • once it does and I go back to my application... bang ! my app exists, mugshot of the culprit is above
This is because the documentInteractionController fires callbacks, and unfortunately in my application the class was released and no longer available. Since the class is no where to be found, the OS cannot call the callback methods and we get the above error. (callbacks are methods that fire after certain events, in this case the method "didEndSendingToApplication" fires to let us know that the 3rd party application got my file)

-To rectify:
  • I had to retain the documentInteractionController (and also the parent view due to some specifics of my application)
  • Release it after the callbacks were fired.
Read a bit on memory management in iOS and ARC (automatic reference counting)


You basically the 'retain' keyword to keep your objects around and then 'release' it after you are done. 
Make sure you cover all paths if you do so, you are interfering with the memory management, so cover your tracks well and avoid memory leaks. Even though you might not see any visible side effects,  we should always write quality code :) 

Hope this helps someone :)

2 comments:

  1. The term garbage collector is a bit misleading on cocoa & cocoa touch as there is no garbage collector. Due to the absence of a garbage collector we have to take care of allocating and deallocating memory. In the case of ARC the burden is some what reduced as the release statements are automatically added at compile time.

    ReplyDelete
  2. Agreed ! Thanks Janidu, changed it to 'memory management'.

    ReplyDelete