Long time no see. Been busy. I have been playing Borderlands (farming crawmerax), doing lots of advanced android learning and just started in getting my hands dirty with Iphone development. So what was my first impression of Objective-C, well man this language is so so, UGLY. Yeah I didn’t feel comfortable with whole syntax in beginning and still coping up with it. But the learning experience has been good and ugly till now, challenging and especially memory management and memory management will be the topic of this post.

Here I will be telling about some of concepts that I learned or am still learning and problems that I faced as a beginner. I am from java background and hadn’t dealt with managed codes so “memory management” was a new candy for me. So I’ll start from the code that gave me a hell-of –a-start information on the topic.

-(void) viewDidLoad{
        	[super viewDidLoad];

        	self.favoriteFilms = [[NSArray alloc] initWithObjects: @”The Lost Boys”, @“Remember The Titans”, nil];
}

I was getting a memory leak on line where I was allocating and initializing my “favoriteFilms” array. Well, the excitement thrill passed through me when I got the exception and crashed :D.
Well so I pulled up my sleeve and got into the work to find the reason and fixing the stuff.
First thing that came to my mind was “My object wasn’t getting released” and that object was “favoriteFilms”. So here are the things that I did:

1) I called “autorelease” on “favoriteFilms” after initializing array and assigning reference to favoriteFilms. Yes, ofcourse memory leak was still there. I got this error/warning: “autorelease sent too many times”. I am still very new to “Objective C memory management” and learning, so at that time, from this error I concluded that autorelease has already been called somewhere. So again calling “autorelease” didn’t worked.

2) I called “release” on “favoriteFilms”. Still the “memory leak” was dancing. It seemed that “retain count” was still above 0 even after calling “release” or I was calling “release” on wrong thing or something else.

3) Lastly I override dealloc method and called “release” on “favoriteFilms” there and yeah “memory leak” was still trolling me 😛

Then I did something, to be honest I didn’t knew why it worked that time but it worked. So I changed my code to something like this:

self.favoriteFilms = [[[NSArray alloc] initWithObjects: @”The Lost Boys”, @“Remember The Titans”, nil] autorelease];

Hmmm what was that ? It worked?…Well it worked! So what I could tell was instead of calling “autorelease” on “favoriteFilms” object, I was supposed to call on the array object that I allocated. Hmmm, I never understood myself what the above conclusion meant. I mean the array that I allocated is also the reference by “favoriteFilms” object. Isn’t it? So why would “autorelease” call on “favoriteFilms” gave error while calling on allocated object itself worked. So let’s again go back to the error/warning: “autorelease sent too many times”. So it could mean this that when I am calling “autorelease” on “favoriteFilms” then getter method is already returning an autoreleased object to me and I am calling “autorelease” again on it 😀 Someone might shed some more light on this 😛

Now the second way worked too. Here is the second way to improve the code:

NSArray *flims = [[NSArray alloc] initWithObjects: @”The Lost Boys”, @“Remember The Titans”, nil];
self.favoriteFilms = films;
[films release];

How is above different than original code ? How did it fix our memory leak issue? What it seemed to me was that I allocated the array and stored its reference on local variable “films”, now when I assigned “films” to “favoriteFilms” then setter method is retaining the object. After that “release” was called on “films” because retain count is 2 now so you have to release it once it’s been used. The concept is still not fully accurate to me so I could be wrong here. Doing more studying on this topic. But from the above second way, I concluded that it is best to approach this way as its more safer and elegant, allocating and assigning object directly to member variable of class might give you trouble if you don’t take measures, so creating the local variable is safer.

Well I have been following this till now so no issues have been faced yet. I am still new and learning and enjoying IOS and Objective-C . As I found one saying on Google “PROGRAMMING LANGUAGES ARE LIKE GIRLFRIENDS: THE NEW ONE IS BETTER BECAUSE YOU ARE BETTER”

Gracias