MavEtJu's Distorted View of the World

Apple iPhone guidelines train lazy programmers

Posted on 2010-05-20 08:00:00
Tags: Apple, iPhone, Objective-C, Rant, Coding

As stated before, I am pretty new to Objective-C programming. Normal C? Not a problem at all! But this object memory management process in Objective-C still gives me a hard time.

The first attempt was simple: I added a lot of NSLog() messages in my dealloc() methods and see which ones I expected to see were not showing up. That were for example all my inherited UIViewControllers... They were not released after they were closed. It is a hard way to do it like this, but it gives you a feel of what you are are really doing wrong.

Then I had a look at the "Leaks" analyze tool in Objective-C (Run -> Analyze -> Leaks) and ran my program. At the end, after a couple of refreshes, it consumed 5 Mb of memory. 5 Mb, is that such a deal? Yes, it is when all the program does is retrieve an XML feed and extracts some data. Oh, and every time it refreshed it lost another huge chunk of memory. Yes, that is a big deal. Luckely the "Leaks" analyze tool tells me which kind of objects it is leaking and when, so you might be able to plug them if you go methodically through the features of your program.... Checking the retainCounters is the way to go.

The next step was to implement a button which terminates the program. Apple doesn't permit you to have apps in their AppStore which have this feature, their usability design is: "If you want to terminate a program, you press the big round Home button at the bottom of the screen.". This breeds lazy programmers, there is no way to properly deallocate all the objects you have. And the excuse you read on the forums about it is "When you terminate your program, the operating system will take care of that.". Absolutely correct, but with some-form-of-multitasking around the corner the current system of "Short living apps won't be able to hog a lot of memory" is over. Plus that it will not show the lazy programmer that he has a serious problem in his code!

And the last tool I thought about was something like a static analysis tool, like Clang (I didn't know this at that moment, and have not used it yet, but it is included in XCode these days). I found a front-end for it called the Analysis Tool at and ran my code through it. Four hundred warnings, and most of them related to objects not being freed.

What I learned from this was: Always "autorelease" the local objects. Assignments increase the retainCounter. Check the retainCounters of the objects you release in your dealloc() methods. Don't trust yourself. And wait for the day that you will be able to see at termination of your program what you have leaked.

To be continued.

| Share on Facebook | Share on Twitter
Comments: No comments yet
Leave a comment
Back to the main page