FROM : Paul Archibald
DATE : Fri Jun 27 22:22:17 2008
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.
DATE : Fri Jun 27 22:22:17 2008
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.
| Related mails | Author | Date |
|---|---|---|
| Paul Archibald | Jun 27, 22:22 | |
| Andy Lee | Jun 27, 22:43 | |
| Rob Ross | Jun 27, 23:41 | |
| Daniel Richman | Jun 28, 02:19 | |
| Clark Cox | Jun 28, 02:59 | |
| Graham Cox | Jun 28, 06:21 | |
| Graham Cox | Jun 28, 06:29 |






Cocoa mail archive

