Re: retaining arrays
Re: retaining arrays
- Subject: Re: retaining arrays
- From: Drew McCormack <email@hidden>
- Date: Fri, 21 Sep 2001 21:56:57 -0700
When you call a factory method, like arrayWithCapacity:, you are
returned an autoreleased object. Unless you retain it, it will be
deallocated at the end of the current event loop. You thus need to
either retain it in the main program, or in the initWithArray: method.
Drew McCormack
On Friday, September 21, 2001, at 10:37 AM, Robert S Goldsmith wrote:
Hi :)
This is confusing me:
1)I create an NSMutableArray in main.m
2)I pass this to the init method of a class instance.
3)The class instance copies this pointer and stores it.
a quick mock up of the code:
in main.m
-----
NSMutableArray *temp;
testClass *classInstance;
temp=[NSMutableArray arrayWithCapacity:2];
[temp addObject:@"String 1"];
[temp addObject:@"String 2"];
classInstance=[[testClass alloc] initWithArray:temp];
Then in testClass
-----
-(id)initWithArray:(NSArray *)theArray
{
myPointerToArray=theArray; //myPointerToArray is
//an instance variable
//if testClass
Now I find that this 'eats' the array in main (it seems to send it a
release message or something). To stop a sigsegv error later on (like
if I tried to release the array temp) I have to send temp a retain
message at some point (either in main before it is passed to the init
call of the instance of testClass or in the init method before the
pointer is copied to myPointerToArray).
Why?
Robert
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev