malloc:mmap(size=XX) failed (error code=12)

Posted by Michel on Stack Overflow See other posts from Stack Overflow or by Michel
Published on 2012-10-13T01:55:03Z Indexed on 2012/10/17 5:02 UTC
Read the original article Hit count: 189

I have a memory problem in an iPhone app, giving me a hard time. Here is the error message I get:

malloc: * mmap(size=9281536) failed (error code=12) * error: can't allocate region

I am using ARC for this app, in case that might be useful information. The code (below) is just using a file in the Bundle in order to load a core data entity.

The strange thing is the crash happens only after more than 90 loops; while it seems to mee that since the size of the "contents" in getting smaller and smaller, the memory request should also get smaller and smaller.

Here is the code, if any one can see a flaw please let me know.

NSString *path,*contents,*lineBuffer;
path=[[NSBundle mainBundle] pathForResource:@"myFileName" ofType:@"txt"];
contents=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

int counter=0;

while (counter<10000) {
    lineBuffer=[contents substringToIndex:[contents rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location];
    contents=[contents substringFromIndex:[lineBuffer length]+1];
    newItem=[NSEntityDescription insertNewObjectForEntityForName:@"myEntityName"
                                                      inManagedObjectContext:context];
    [newItem setValue:lineBuffer forKey:@"name"];

    request=[[NSFetchRequest alloc] init];
    [request setEntity: [NSEntityDescription entityForName:@"myEntityName"
                         inManagedObjectContext:context]];
    error=nil;
    [context save:&error];

    counter++;
}

© Stack Overflow or respective owner

Related posts about iphone-sdk-4.0

Related posts about memory-leaks