Feb 05

I was seeing this error message when testing an app on a device:

Mashable(1870,0x3940f7e0) malloc: *** mmap(size=3802099712) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

Trying to allocate 3.8 GB of memory on an iPhone isn’t likely to succeed, although the simulator is all to happy to comply.

After some debugging I found this code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numRows;
	
    NSArray *headlines = [[BlogService sharedInstance] headlines];
    if (headlines) {
        numRows = [headlines count];
    }
	
    return numRows;
}

Ouch! Returning a variable that has not been initialized and is therefore pointing to a random location in memory is not going to lead to anything good.

Moral of the story: Always initialize your variables. Test early and often on a device.

written by Nick \\ tags: