Comrades:

I have experimented a bit with this, and I think I have something that works, but I would like to understand it better. I am reading the Hillegass book, but I don't really see an example of what I am tryng to do, although it seems like a very basic question.

The Question:
If I want to create an object and pass it to another method, where I plan to use it briefly (as a local variable), what is the proper way to allocate and deallocate the object in the various methods?


Here is a simple example:

-(NSArray*) makeObject {
NSArray *a = [NSArray arrayFromObjects];someObject, anotherObject, nil];
        // should I [a retain];
        // or [a release];
        // or [a autorelease];
        // or do nothing
        // before I
        return a;
}

-(void) useObject {
        NSArray *local = [self makeObject];
        // should I [local retain];
        // or [local release];
        // or [local autorelease];
        // or do nothing
        // before I exit this method?
// I need to pass local as an argument to the creation of another object before exiting
        MyObj *o = [[MyObj alloc] initWithArray:local];
}


There are a lot of possible combinations here, and I have tried a bunch of them. Some of them work, others crash the program with a EXC_BAD_ADDRESS. But, as I have been trying to make an exhaustive test of what "works" and what "crashes", I realize that just experimenting with this is not enough, and I need a better theoretical understanding of this stuff. If you recognize this pattern and can tell me where to read more on it, that would be great, too.
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to