Skip navigation.
 
mlsimple question about passing around pointers to objects
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.

Related mailsAuthorDate
mlsimple question about passing around pointers to objects Paul Archibald Jun 27, 22:22
mlRe: simple question about passing around pointers to objects Andy Lee Jun 27, 22:43
mlRe: simple question about passing around pointers to objects Rob Ross Jun 27, 23:41
mlRe: simple question about passing around pointers to objects Daniel Richman Jun 28, 02:19
mlRe: simple question about passing around pointers to objects Clark Cox Jun 28, 02:59
mlRe: simple question about passing around pointers to objects Graham Cox Jun 28, 06:21
mlRe: simple question about passing around pointers to objects Graham Cox Jun 28, 06:29