The House of Witten

Wednesday, April 27, 2005

More Cocoa

So here is the current state of things. I ran through that tutorial. Lovely. I also borrowed "Cocoa Programming for Mac OS X" by Aaron Hillegass from the library, and have started reading that. Sadly, this has freaked me out a bit, since you are rather quickly introduced to the idea of Cocoa's memory management approach, which involved reference counting, something I have no experience with. This includes the need to retain and release objects that you are using, and the idea of autorelease, which I don't really understand yet. The idea is that when an object is created (using alloc and init) it has this internal reference counter that is incremented. So long as this value is not zero, the object sticks around. As soon as it drops to zero (the release command decrements the counter by 1 each time it's called), the object is deallocated. Seems easy enough. The hard thing to learn is when this internal reference thing is incremented. Evidently, when an object is added to an array, the object's reference count is incremented, and when that array is destroyed, the reference count of each object in that array is decremented. This gets confusing though (you really need to read the book to get why), since you must release an object right after you add it to an array, to avoid a memory leak that will arise from the extra reference that was made when the object was first allocated.

Then there's other stuff dealing with objects and scope. If you have some function that assigns some object to some other object, and since everything is dealt with by reference, there is a need to both release an object before leaving the current function (going out of scope won't kill it, since only the pointer will be deallocated, not the memory/object that it points to), and also to retain it so that the assignment will work (you can't assign one object to another if the source object has just been deallocated due to issuing a release). Anyway, to get around this problem you use autorelease, but I don't yet understand when exactly autorelease kills the source object. Maybe as I read on in the book it will become clearer, but for now I don't really get it (I understand why it's needed, and the whole retain and release thing, but not this autorelease thingy). There's also this NSAutorelease object called pool that seems to be relevant, but again, I'm not yet sure why. I'm about to read a webpage that might clarify things (http://www.cocoadev.com/index.pl?RetainingAndReleasing), and I'll post my progress as in a bit.

On a side note, there is this other cool aspect of Cocoa called bindings, which I'm also just starting to understand. The big picture idea is that the interface (created with interface builder) can do a lot of stuff on it's own, without the help of any so-called "glue code." Things like an slider changing the value of a text field, or a color picker changing the color of some text, can all be done without any actual code. This is quite cool, but again, I don't get it yet (I need to run through this other Apple tutorial)

0 Comments:

Post a Comment

<< Home