Session #2 summary

1 view
Skip to first unread message

Radamanthus Batnag

Feb 18, 2010, 11:30:18 AM2/18/10

Session 2 was conducted on the same venue as, and right after the PhRUG meetup.


There were two new attendees, tin and nico, who promised to catch up with the previous assignments.

We discussed memory management.

When should you call release on an object? When you’re the one who did the alloc for the object.

When should you not call release? When you did not do the alloc for the object.

Why bother with alloc and init at all, when, for example, you can use class methods that do it for you?
For example, why use:

NSString *my_string = [[NSString alloc] initWithFormat:@"format"];

when you can instead the format below?

NSString *my_string = [NSString stringWithFormat:@"format"];

Answer: No reason at all. Use the latter if it suits you. But be sure you know alloc/init/release, because when you create your own class initialization methods, you will have to use them. For example, for the PolygonShape assignment, we can have a method like the one below:

[PolygonShape polygonWithNumberOfSides:5];

to save the user the need to do the alloc/init dance. But inside the method above, we will have to do the alloc/init/autorelease dance.

As before, let’s try to do Week 3 in one week. That means lectures 5 (Views, Animation, OpenGL) and 6 (View Controllers), and assignment 3 (HelloPoly II). Let’s aim to view the lectures and finish the assignment by next week and have a meetup Thursday next week, February 25.

For those of you catching up and still working on previous assignments, feel free to post questions about them, both on the forum and on meetups.

Eumir Gaspar

Feb 18, 2010, 7:26:31 PM2/18/10
I just remembered im on vacation from 24-26 haha. But if everyone can finish by then, go ahead :) I'll just read the post meetup notes :)
Eumir Gaspar
Web Developer
Message has been deleted


Feb 18, 2010, 7:35:41 PM2/18/10
to cocoaheads-manila
Thanks for the updates Rad!

I agree that the stringWithFormat and initiWithFormat methods are more
or less the same in most cases but if your application is NSString
intensive and you'd like to control when you want the objects to be
released then you might want to prefer the alloc/initWithFormat
approach. But if it's alright with you to wait for the event loop to
finish before the autorelease pool is drained then stringWithFormat
should just work out fine and is a bit cleaner too.

Below is an excerpt from the Beginning iPhone Development book:

"This code would work exactly the same as the code we used. Class
methods like this one are called convenience or factory methods, and
they return an autoreleased object. Following the general memory rule
that “if you didn’t allocate it or retain it, don’t release it,” these
autoreleased objects don’t have to be released unless you specifically
retain them, and using them often results in code that’s a little
shorter and more readable. But, there is a cost associated with these
convenience methods because they use the autorelease pool. The memory
allocated for an autoreleased object will stay allocated for some
period of time after we’re done with it. On Mac OS X, with swap files
and relatively large amounts of physical memory, the cost of using
autoreleased objects is nominal, but on iPhone, these objects can have
a detrimental effect on your application’s memory footprint. It is OK
to use autorelease, but try to use it only when you really need to,
not just to save typing a line or two of code."

Also, regarding the polygonWithNumberOfSides method that you
suggested, I think there's a naming convention that Apple suggests. If
you use alloc/initWithNumberOfSides then you should return a non-
autoreleased object. If you name the class method
polygonWithNumberOfSides then it should return an autoreleased object
and the caller should make sure that retain is also called otherwise
the object is destined for neverland after the event loop/pool is


Radamanthus Batnag

Feb 18, 2010, 8:10:35 PM2/18/10
In short, we use alloc/init when we want to be explicit on when we do the release.
And on the iPhone OS, we want that most of the time.

Thanks for the clarification, paul!

I read that part of the book, too, but I only understood it during this discussion. :)

On Feb 19, 2010, at 8:31 AM, paul_sns wrote:

Below is an excerpt from the <a href="
iPhone Development</a> book:

ruben d canlas jr

Feb 18, 2010, 9:40:02 PM2/18/10
It looks like I have lots of things to catch up with. Thanks, Radam, for the summary.

"When your computer wont power up always complain to IT before checking to see if you've switched on the monitor"

On Fri, Feb 19, 2010 at 12:30 AM, Radamanthus Batnag <> wrote:
Reply all
Reply to author
0 new messages