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:
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.
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
Below is an excerpt from the <a href="http://www.amazon.com/gp/product/
iPhone Development</a> book: