Effective memory class

I am creating an array of dictionaries (called keys) in an iphone application to store section names and line counts to represent a table.

The code is as follows:

[self.results removeAllObjects];
[self.keys removeAllObjects];

NSUInteger i,j = 0;
NSString *key = [NSString string];
NSString *prevKey = [NSString string];

if ([self.allResults count] > 0)
{
    prevKey = [NSString stringWithString:[[[self.allResults objectAtIndex:0] valueForKey:@"name"] substringToIndex:1]];

    for (NSDictionary *theDict in self.allResults)
    {
        key = [NSString stringWithString:[[theDict valueForKey:@"name"] substringToIndex:1]];

        if (![key isEqualToString:prevKey])
        {
            NSDictionary *newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [NSNumber numberWithInt:i],@"count",
                                           prevKey,@"section",
                                           [NSNumber numberWithInt:j],
                                           @"total",nil];

            [self.keys addObject:newDictionary];
            prevKey = [NSString stringWithString:key];
            i = 1;
        }
        else
        {
            i++;
        }
        j++;

    }

    NSDictionary *newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                   [NSNumber numberWithInt:i],@"count",
                                   prevKey,@"section",
                                   [NSNumber numberWithInt:j],
                                   @"total",nil];

    [self.keys addObject:newDictionary];

}

[self.tableview reloadData];

The code works fine the first time, but sometimes I have to rebuild the entire table, so I repeat this code, which is great for the simulator, but on my device the program bombs when I execute the reloadData line:

malloc: *** mmap(size=3772944384) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
malloc: *** mmap(size=3772944384) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Program received signal:  "EXC_BAD_ACCESS".

If I delete the reloadData line, the code works on the device.

I am wondering if this is due to the way I built the key array (i.e. using auto-implemented strings and dictionaries).

+3
source share
3 answers

, :

malloc: *** mmap(size=3772944384) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
malloc: *** mmap(size=3772944384) failed (error code=12)
*** error: can't allocate region

, 3 772 944 384; 4 . malloc() -, malloc , (mmap) 4 !

, , , davydotcom, , . , ? , - .

, malloc_error .

, :

NSString *key = [NSString string];
NSString *prevKey = [NSString string];

.

- , Objective-C guide Cocoa .

self.keys self. self.results (, retain, ).

.

+15

, , . , .

2 , self.keys self.results

self.keys self.results , removeAllObjects.

0

An array of dictionaries called keys ... [self.keys addObject: newDictionary] .. required .. false .. down.

0
source

Source: https://habr.com/ru/post/1747576/


All Articles