Re: Allocation and disposal inside a loop
Re: Allocation and disposal inside a loop
- Subject: Re: Allocation and disposal inside a loop
- From: email@hidden (Simon Fraser)
- Date: Tue, 4 Feb 2003 10:13:54 -0800
On Monday, February 3, 2003, at 09:08 pm, Ken Tozier wrote:
I've managed to confuse myself about what will happen in the following
code. Could anyone tell me whether the "release" method call at the
bottom of the loop trashes items that have already been placed in the
array?
No. The array adds a reference count on the object when you call
[NSMutableArray addObject:].
So
[MyClass alloc] gives you an object with a refcount of 1
[theArray addObject: tempObject] increases that refcount to 2
[tempObject release] reduces the refcount to 1, leaving the object
owned by the array.
BTW, you should declare the *tempObj variable inside the loop, for
clarity:
NSMutableArray *theArray = [[NSMutableArray alloc] init];
while (startPtr < endPtr)
{
MyClass *tempObj = [MyClass alloc]; // should this do [[MyClass
alloc] init] ?
...
NSMutableArray *theArray = [[NSMutableArray alloc] init];
MyClass *tempObj;
while (startPtr < endPtr)
{
tempObj = [MyClass alloc];
if ([tempObj initializer1:&startPtr end:endPtr] != nil)
[theArray addObject:tempObject];
else if ([tempObj initializer2:&startPtr end:endPtr] != nil)
[theArray addObject:tempObject];
else break;
startPtr += [tempObj length];
[tempObject release];
}
Thanks,
Simon
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.