The progress continues

Today I have implemented the menu with buttons that is used at several places in the app. In the fights you have the Sort functions, access chat etc so it’s a must have. The interesting thing is that Android devices have a physical Menu-button and a built in menu handler but since iPhones don’t we need to have both a visual button in the interface whenever there’s a menu to use and a custom made menu object to take care of the buttons.

Anyway, it’s in place now and it works. This means that I have finally been able to test the sort functions and the replay functions and besides some memory failures I fixed, they now work as they should.

Memory… yes. 
That’s a real pain in the ass when it comes to Objective C. However I do think I begin to understand it finally. The bad thing is that everything I have coded so far is wrong so I need to go through all code now to make it work right. I don’t want leaking memory or an app that crashes every 10th time it’s used.

But to sum up, I can tell you what I’ve learned so far.

1. Whenever you alloc something, make sure to release it too but NEVER dealloc it directly. This is something Objective-C will do for you as soon as the retain count is zero. Trust Objective-C on this.

2. If you have an object which you send another object to (like a NSString or an array), make sure to either retain it or copy it. I have found that I prefer to copy strings since I then know that they are solely owned by the object and that noone else fiddles with then anywhere else.

3. There’s a HUGE difference between using Name = @”Kalle”; and self.Name=@”Kalle”; in a class. In the first case Name will pointing to the string “Kalle” which is an autorelease object (that’s what the @ is for). In the latter case Name will use the method defined in the @property definition of the class. This means that if you have written  @property (copy) NSString *Name; in the class definition and then use self.Name = @”Kalle”; your variable Name will point to a COPY of the string “Kalle” instead of the autorelease string containing “Kalle”. I hope you see the difference.

4. When adding objects to arrays, you can (and should) release them afterwards since the array owns them now. Example:

Tile *pTile = [Tile alloc];
[MyArrayOfTiles addObject:pTile];
[pTile release];

Line 1 allocates one instance of a Tile and gives it retain count 1
Line 2 adds pTile to MyArrayOfTiles, increasing it’s retain count to 2 (object is owned both by the array and my main function)
Line 3 releases pTile in my main function, which leaves it owned by the array with a retain count of 1.

This is good since if we do [MyArrayOfTiles removeAllObjects], all tiles added to the array receives a retain count of 0 and is then deallocated by the system.

Are you confused yet?
I know I am. Thank god all this shit does not apply to simple numbers and similar.

More to come but now it’s time for bed.

0 Responses to “The progress continues”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: