Re: Leak problem in Cocoa NSMutableArray/objectEnumerator
Re: Leak problem in Cocoa NSMutableArray/objectEnumerator
- Subject: Re: Leak problem in Cocoa NSMutableArray/objectEnumerator
- From: Fritz Anderson <email@hidden>
- Date: Fri, 27 Aug 2004 10:10:05 -0500
This isn't an Xcode question, but here's your problem:
Read up on autorelease pools.
<http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
Concepts/AutoreleasePools.html>.
Autoreleased objects are not deallocated immediately, but are held in
the current NSAutoreleasePool for release when the current pool is
deallocated. There is only one default pool, in the run loop of the
main thread, which gets released at each pass through the run loop
(recent discussions suggest only after user events are processed).
Your code allocates autoreleased objects (the NSEnumerator certainly,
and probably some temporaries inside NSLog) but never returns to the
run loop. The current autorelease pool is never released, so nor are
the millions of objects you allocate.
The solution would be to bracket your inner loop with its own
NSAutoreleasePool:
for (i=0;i<10000;i++)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
for (j=0;j<1000000;j++)
k = [self traverseArray] ;
// report every 1 000 000 operations
NSLog(@"Done %i M Iterations", i) ;
[pool release];
}
Depending on your application, you might want to make pool
reallocations more or less frequent -- the overhead of releasing an
autorelease pool is U-shaped with respect to object count.
If your simulation really is as intensive as your example, your human
interface will be unresponsive for a long time -- event handling is
another thing you have to return to the run loop to enable. If this is
a problem, you'll have to break the simulation into another thread.
-- F
On 27 Aug 2004, at 5:52 AM, Professor Leslie Smith wrote:
startbutton repeatedly traverses the array, reporting every million
traverses:
- (void) startButton: sender
{
int i,j,k ;
// repeatedly traverses the array
for (i=0;i<10000;i++)
{
for (j=0;j<1000000;j++)
k = [self traverseArray] ;
// report every 1 000 000 operations
NSLog(@"Done %i M Iterations", i) ;
}
}
and traverseArray looks like
- (int) traverseArray
{
id anObject ;
int j ;
// traverse array
NSEnumerator *enumerator = [MyArray objectEnumerator] ;
while (anObject = [enumerator nextObject]) {
j = [anObject intValue] ;
/* code to act on each element as it is returned */
}
return j ;
}
What happens is
... (lines deleted)
2004-08-27 09:57:01.693 tester[2662] Done 52 M Iterations
2004-08-27 09:57:26.595 tester[2662] Done 53 M Iterations
Program received signal: "SIGTRAP".
*** malloc: vm_allocate(size=1069056) failed (error code=3)
*** malloc[2662]: error: Can't allocate region
I don't think this program should not fail at all.
It looks as though the NSMutableArray objectEnumerator is not being
deallocated entirely.
This is a real difficulty if one is to use NSMutableArray as one would
wish - in my own case, in a long simulation.
I have reported this as a bug: (3778469).
I haven't had time to try it out on a plain NSArray.
Is this a known problem? Has anyone a workaround? (preferably short of
using C arrays).
(Or have I simply misunderstood something?)
--
Professor Leslie S. Smith,
Dept of Computing Science and Mathematics,
University of Stirling,
Stirling FK9 4LA, Scotland
email@hidden
Tel (44) 1786 467435 Fax (44) 1786 464551
www http://www.cs.stir.ac.uk/~lss/
UKRI IEEE NNS Chapter Chair: http://www.cs.stir.ac.uk/ieee-nns-ukri/
--
The University of Stirling is a university established in Scotland by
charter at Stirling, FK9 4LA. Privileged/Confidential Information may
be contained in this message. If you are not the addressee indicated
in this message (or responsible for delivery of the message to such
person), you may not disclose, copy or deliver this message to anyone
and any action taken or omitted to be taken in reliance on it, is
prohibited and may be unlawful. In such case, you should destroy this
message and kindly notify the sender by reply email. Please advise
immediately if you or your employer do not consent to Internet email
for messages of this kind.
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.
_______________________________________________
xcode-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/xcode-users
Do not post admin requests to the list. They will be ignored.