PDA

View Full Version : Learning Cocoa?


pmazer
2005-11-17, 16:48
I'm an experienced programmer who wants to get started writing in Objective C and Cocoa. Does anyone have any suggestions for reading material to learn both of these?

Brad
2005-11-17, 16:52
The O'Reilly books always get high praise. I picked up Aaron Hillegass' Cocoa Programming for Mac OS X from the library a while back and I thought it was pretty good. I intend to pick it up again when I have time to really dive into some code. He also has a book titled Advanced Mac OS X Programming, but I haven't read that.

pmazer
2005-11-23, 04:05
I'm currently reading through Aaron Hillegass' book, and it seems to be doing a good job. One of the only things I don't quite get yet, though, is NSAutoreleasePool. In one of the examples (a Foundation Tool), he creates an NSAutoreleasePool in the main function, and calls a method inside. In that method, an object is created and then autoreleased so it can be returned. Is that object released after it is returned, or after the pool is released?

Mr Beardsley
2005-11-23, 11:32
I'll try to answer your question as best I can.

Have you worked through retaining and releasing objects yet? If so all NSAutoReleasePool does, is to hold on to objects and release them at a later time. If you create an object and you just need it to exist in your method, you can add it to the AutoReleasePool, and it will be deallocated sometime after you return from that method. At some point on the run loop the pool will be sent a message to release all its objects, at which point the pool sends the release message to all the objects it contains and they are deallocated.

When you are writing an app that uses AppKit, setting up the AutoReleasePool and clearing it is all handled for you. I'm fairly certain that both foundation tools and new threads require you to create an AutoReleasePool. You also have to set up clearing the pool. You can call this whenever you would like, or just set it up to be called once everytime you go around the run loop.

Hopefully that made sense.

carnellm
2005-11-23, 12:36
I'm an experienced programmer who wants to get started writing in Objective C and Cocoa. Does anyone have any suggestions for reading material to learn both of these?
I would have to agree that Cocoa Programming for Mac OS is the best place to start. Also, don't forget Apple's Developer site. There are some great explanations and tutorials there.

pmazer
2005-11-23, 12:44
I'll try to answer your question as best I can.

Have you worked through retaining and releasing objects yet? If so all NSAutoReleasePool does, is to hold on to objects and release them at a later time. If you create an object and you just need it to exist in your method, you can add it to the AutoReleasePool, and it will be deallocated sometime after you return from that method. At some point on the run loop the pool will be sent a message to release all its objects, at which point the pool sends the release message to all the objects it contains and they are deallocated.

When you are writing an app that uses AppKit, setting up the AutoReleasePool and clearing it is all handled for you. I'm fairly certain that both foundation tools and new threads require you to create an AutoReleasePool. You also have to set up clearing the pool. You can call this whenever you would like, or just set it up to be called once everytime you go around the run loop.

Hopefully that made sense.

Wow, that's almost exactly what the book said, scary :lol:. The thing I don't understand is the sentence: "At some point on the run loop the pool will be sent a message to release all its objects, at which point the pool sends the release message to all the objects it contains and they are deallocated." Is this when the pool is deallocated, or just a random spot in time which no one knows about?

thuh Freak
2005-11-23, 14:06
Wow, that's almost exactly what the book said, scary :lol:. The thing I don't understand is the sentence: "At some point on the run loop the pool will be sent a message to release all its objects, at which point the pool sends the release message to all the objects it contains and they are deallocated." Is this when the pool is deallocated, or just a random spot in time which no one knows about?
its something like this:
while( run_loop_still_goes ) {
NSAutoreleasePool *apool = [NSAutoreleasePool new];

// lots of code. ya, know. doin' stuff
NSObject *myobj = [[[myobj alloc] init] autorelease];
// blah

[apool release]; // this releases the apool, and myobj, and anything else in the autorelease pool
}

intuition would imply that the apool's release happens at the end of loop. But, i haven't actually looked at apple's specific implementation. The pool might infact live in a larger scope, like just outside the main runloop, ie:

NSAutoreleasePool *apool = /*new*/;
while(run_loop) {/*blah blah.. put something on the autorelease pool*/}
[apool release];


its certainly not gonna be random ;), but i aint sure if its released every loop, or right after the loop. when i spawn my own threads, sometimes i put it on the inside of the runloop, sometimes on the outside.

Mr Beardsley
2005-11-23, 14:30
This is probably good to read and may help answer questions that I can't.

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/Classes/NSAutoreleasePool.html

For your question about when things happen with autoreleasepool, it is created as one of the first actions in the run loop. The run loop can either be the one provided by the AppKit, or it may a loop that you make yourself. If you create a custom run loop, the first thing you have to do is make your autoreleasepool. In the foundation tool that you are made for the book there is no loop that I remember so you have to create the pool as your first action in main. Then, as the last action in your loop you release the pool, which in turn releases all the objects. By releasing the pool at the end of your loop you are guaranteed not to release an object that is till in the scope of one of your methods.

You can create as many autorelease pools as you want, and if you have a lot of objects allocated in a loop, you may choose to make an autoreleasepool for just that loop rather than adding all those objects to the main pool and waiting for it to be released.

pmazer
2005-11-23, 15:58
Thanks that helps a lot! But I still have one more question:

If the loop is:
NSAutoreleasePool *pool = [NSAutoreleasePool new];
for (int i = 0; i < 3; i++) {
NSObject *object = [object method];
}
[pool release];
And the method is:
- (NSObject *)method
{
NSObject *obj = [[NSObject alloc] init];
[obj autorelease];
return obj;
}

When is obj release? When the method is returned or when then pool is released?

Mr Beardsley
2005-11-23, 18:44
Obj is released when the pool is released.

I see you are following Apple's guidelines of autoreleasing any object you create and then return from a method. When you get an object back from a method you assume that it has been autoreleased and the first thing you do is retain it.

Suppose you have the following method:

- (id) someMethod
{
return [[[obj alloc] init] autorelease];
}


When you get that object out the first thing you'll do is retain it.

myObj = [object someMethod];
[myObj retain];


That sets the retain count to 2. After the autoreleasepool is released the retain count goes down to 1. Then when you are finished with the object you would call

[myObj release];


After that the object is deallocated.

pmazer
2005-11-23, 20:56
Ah, okay, that helped a lot, thanks