retaining arrays
retaining arrays
- Subject: retaining arrays
- From: Robert S Goldsmith <email@hidden>
- Date: Fri, 21 Sep 2001 18:37:10 +0100
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