• 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
NSEnumerator behaves unexpectedly
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSEnumerator behaves unexpectedly


  • Subject: NSEnumerator behaves unexpectedly
  • From: Hans van der Meer <email@hidden>
  • Date: Sun, 5 Nov 2006 19:03:05 +0100

I found a strange memory behaviour when using enumerations. But I hesistate to cry "bug".

In the next code fragment three objects are created and added to an array. I expect them to have retain count 2 after insertion in the array and that is indeed the case.
Then I did an enumeration over the array, and lo and behold, the last item got an increased retain count. This increased retain count turns out to persist until the next cycle in the autoreleasepool.


ObjC code:
unsigned int i; NSNumber *num;
NSNumber *num1 = [[NSNumber alloc] initWithInt:1101];
NSNumber *num2 = [[NSNumber alloc] initWithInt:1102];
NSNumber *num3 = [[NSNumber alloc] initWithInt:1103];
NSArray *array = [NSArray arrayWithObjects:num1,num2,num3,nil];
for (i = 0; i < 3; ++i)
NSLog(@"loop: retaincount number %@ = %u", [array objectAtIndex:i], [[array objectAtIndex:i] retainCount]);
NSEnumerator *arrayEnumerator = [array objectEnumerator];
while (num = [arrayEnumerator nextObject]) {
NSLog(@"enumerator object num %@", num);
for (i = 0; i < 3; ++i)
NSLog(@"loop %u: retaincount number %@ = %u", i, [array objectAtIndex:i], [[array objectAtIndex:i] retainCount]);}


Output:
2006-11-05 18:53:58.025 test[1957] loop: retaincount number 1101 = 2
2006-11-05 18:53:58.025 test[1957] loop: retaincount number 1102 = 2
2006-11-05 18:53:58.025 test[1957] loop: retaincount number 1103 = 2
2006-11-05 18:53:58.025 test[1957] enumerator object num 1101
2006-11-05 18:53:58.025 test[1957] loop 0: retaincount number 1101 = 2
2006-11-05 18:53:58.025 test[1957] loop 1: retaincount number 1102 = 2
2006-11-05 18:53:58.025 test[1957] loop 2: retaincount number 1103 = 2
2006-11-05 18:53:58.025 test[1957] enumerator object num 1102
2006-11-05 18:53:58.025 test[1957] loop 0: retaincount number 1101 = 2
2006-11-05 18:53:58.025 test[1957] loop 1: retaincount number 1102 = 2
2006-11-05 18:53:58.025 test[1957] loop 2: retaincount number 1103 = 2
2006-11-05 18:53:58.025 test[1957] enumerator object num 1103
2006-11-05 18:53:58.025 test[1957] loop 0: retaincount number 1101 = 2
2006-11-05 18:53:58.026 test[1957] loop 1: retaincount number 1102 = 2
2006-11-05 18:53:58.026 test[1957] loop 2: retaincount number 1103 = 3

When I make the array static, initialize it on the first pass of the test code only, and examine the retain counts after passing the autoreleasepool, then the anomalous count on the last element has disappeared and all elements again have retain count 2.
It is also shown that the higher value of the retain count is set on the last element only, when it is coming out of the enumerator.
Since I have no access to the source I can only guess what causes this seemingly anomalous behaviour of the last element.


Question: Can this be considered a bug or is it just a minor inconsistency and shouldn't I worry about it? The second experiment shows that there seems to be not really a memory leak (even when retaining the enumerator). Otherwise: just curious what happens here in order to better understand the memory management involved.

Hans van der Meer


_______________________________________________ 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
  • Follow-Ups:
    • Re: NSEnumerator behaves unexpectedly
      • From: Charlton Wilbur <email@hidden>
  • Prev by Date: Re: Problem with IB not control-dragging to make connections
  • Next by Date: Re: NSEnumerator behaves unexpectedly
  • Previous by thread: Re: Indicate loading while bindings read from Core Data
  • Next by thread: Re: NSEnumerator behaves unexpectedly
  • Index(es):
    • Date
    • Thread