• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Allocation and disposal inside a loop
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Allocation and disposal inside a loop (From: Ken Tozier <email@hidden>)

  • Prev by Date: Re: Sorting an NSOutlineView
  • Next by Date: NSTask on another processor - is it NSHost?
  • Previous by thread: Re: Allocation and disposal inside a loop
  • Next by thread: Sorting an NSOutlineView
  • Index(es):
    • Date
    • Thread