Tuesday, June 9, 2015

When [NSFileManager defaultManager] became nil

A few days back we faced an interesting problem. To use / manipulate a locally stored file, we were using the [NSFileManager defaultManager] to obtain an instance of the default File Manager, and then were trying to access the file. However, whenever this code fragment was executed the app would crash. We setup up breakpoint(s) to try to isolate the issue, and during debugging it showed up that on the very first line, where we tried to obtain a reference to the defaultManager, the instance was a valid one, but the moment we actually tried to use the reference to do something useful, the instance showed up as nil. We checked online as well, and couldn't find any references to other having a similar problem and spent some time being perplexed by why would the defaultManager be returned as nil. 

After sometime when we didn't make progress on resolving the defaultManager issue, we decided to check on the path to the file that we were trying to access and found that the path was invalid due to a bundling issue, which was in turn invalid because of build script issue. We immediately resolved the build script issue, and subsequently the defaultManager returned a valid reference ( as expected ! ). While we were happy at resolving the issue, we were also hoping that the defaultManager didn't show up as nil in the debugger, which led us to spending quite a bit of time in trying to figure that one out ( which ultimately turned out to be a "ghost" issue distracting us from the real problem ).

No comments: