RE: Array of random, non-repeating numbers
RE: Array of random, non-repeating numbers
- Subject: RE: Array of random, non-repeating numbers
- From: Jeff Laing <email@hidden>
- Date: Fri, 14 Jul 2006 11:02:32 +1000
> From: Nir Soffer [mailto:email@hidden]
> - (void)swapObjectAtIndex:(unsiged)a withObjectAtIndex:(unsiged)b {
> id temp = [self objectAtIndex:a];
> [self replaceObjectAtIndex:a withObject:[self objectAtIndex:b]];
> [self replaceObjectAtIndex:b withObject:temp];
> }
I'm not sure, and I'd like a memory purist to step in and correct me if I'm
wrong, but I think this will have problems if the element in the array has a
retain count of 1.
Lifting object[a] into temp doesn't increase its retain count.
Replacing object[a] in the array should reduce its retain count by 1,
potentially deallocating it
Or am I missing something? I think it needs to be:
- (void)swapObjectAtIndex:(unsiged)a withObjectAtIndex:(unsiged)b {
id temp = [[self objectAtIndex:a] retain];
[self replaceObjectAtIndex:a withObject:[self objectAtIndex:b]];
[self replaceObjectAtIndex:b withObject:temp];
[temp release];
}
This is one of those horrible subtleties of the retain/release scheme that I
just hate.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden