Re: NSMutableArray + NSEnumerator = No Memory
Re: NSMutableArray + NSEnumerator = No Memory
- Subject: Re: NSMutableArray + NSEnumerator = No Memory
- From: "Philip Q" <email@hidden>
- Date: Mon, 24 Sep 2007 19:10:13 +1200
On 23/09/2007, James Bucanek <email@hidden> wrote:
> I was eventually able to isolate the problem; My application is
> running out of memory. The crash occurs at
>
> NSMutableArray* files;
> ...
> NSEnumerator* e = [files objectEnumerator]; <-- crashes in
> CFArrayGetValues > __memcpy
>
I think the problem is that you are using an NSMutableArray:
The contents of a mutable array can be modified at any time, this is
problematic for an enumerator as if the array is modified while it is
enumerating through it, it could end up skipping elements or some
other funky behaviour (even thought the documentation discourages this
type of modification).
The behaviour you're seeing is a way to combat that---make a copy of
all the references in the array at the time of creation of the
enumerator and enumerate though that list (which will never change).
If you make a single copy of the array with [NSArray
arrayWithArray:files] (which is what Chris' MemoryEfficientEnumerator
wraps), and use that for your enumerator, you should get more
memory-efficient, an safe behaviour (DISCLAIMER: I haven't actually
tested this, it just seems sensible).
-Phil
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden