Common Leaks when using Xib files
Xib (nib) files in Cocoa make memory leaks super easy, especially if you are coming from iOS Development. In iOS top level objects are autoreleased, but on the Mac it is the responsibility of the file’s owner to release them.
Fun fact if you don’t use something like NSWindowController or NSViewController you need to release all the top level objects in your nib or they will be leaked.
You can either create an outlet to each top level object individually and release them on dealloc or use
-[NSNib instantiateNibWithOwner: topLevelObjects:] and keep track of the array of topLevelObjects. Managing the top level objects is not that bad, but something that can be easily forgotten.
Things get a bit trickier when you use ARC (automatic reference counting). Since you are not allowed to call release you either need to not use arc when loading nibs with top level objects or cast objects...
Continue reading →