Member
|
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?
|
quote |
Selfish Heathen
Join Date: May 2004
Location: Zone of Pain
|
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.
The quality of this board depends on the quality of the posts. The only way to guarantee thoughtful, informative discussion is to write thoughtful, informative posts. AppleNova is not a real-time chat forum. You have time to compose messages and edit them before and after posting. |
quote |
Member
|
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?
|
quote |
Member
|
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. "Slow vehicle speeds with frequent stops would signal traffic congestion, for instance." uh... it could also signal that my Mom is at the wheel... |
quote |
New Member
|
Quote:
|
|
quote |
Member
|
Quote:
|
|
quote |
Finally broke the seal
Join Date: May 2004
|
Quote:
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. |
|
quote |
Member
|
This is probably good to read and may help answer questions that I can't.
http://developer.apple.com/documenta...leasePool.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. "Slow vehicle speeds with frequent stops would signal traffic congestion, for instance." uh... it could also signal that my Mom is at the wheel... |
quote |
Member
|
Thanks that helps a lot! But I still have one more question:
If the loop is: Code:
NSAutoreleasePool *pool = [NSAutoreleasePool new];
for (int i = 0; i < 3; i++) {
NSObject *object = [object method];
}
[pool release]; And the method is:Code:
- (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? |
quote |
Member
|
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: Code:
- (id) someMethod
{
return [[[obj alloc] init] autorelease];
} When you get that object out the first thing you'll do is retain it. Code:
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 callCode:
[myObj release]; After that the object is deallocated."Slow vehicle speeds with frequent stops would signal traffic congestion, for instance." uh... it could also signal that my Mom is at the wheel... |
quote |
Member
|
Ah, okay, that helped a lot, thanks
|
quote |
Posting Rules | Navigation |
|
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Need Cocoa Help - NSView, NSScrollView, NSClipView | bassplayinMacFiend | General Discussion | 5 | 2005-09-24 08:24 |
Good Book to Learn Cocoa? | dlstarr7 | Genius Bar | 6 | 2005-06-27 11:19 |
Cocoa Programming | adam_tj | Genius Bar | 1 | 2005-01-14 16:49 |
Worth learning 10.4 XCode? | ast3r3x | Programmer's Nook | 15 | 2004-10-07 17:22 |
Learning French the Dumb Way | drewprops | AppleOutsider | 6 | 2004-05-20 22:57 |