2

How could I create the following array without having memory leaks:

It should be an array with arrays inside

 arr = [[NSMutableArray alloc] initWithObjects:
                [[NSMutableArray alloc] init], 
                [[NSMutableArray alloc] init],    
                [[NSMutableArray alloc] init],
                [[NSMutableArray alloc] init],     
                [[NSMutableArray alloc] init], 
                [[NSMutableArray alloc] init],     
                [[NSMutableArray alloc] init], nil];
2
  • +(id)arrayWithCapacity:(NSUInteger)numItems ? Commented Feb 8, 2012 at 16:33
  • 1
    Use [NSMutableArray array] rather than your alloc/init approach. Commented Feb 8, 2012 at 16:33

3 Answers 3

3

Use [NSMutableArray array] instead, which creates an autoreleased object that you don't have to worry about:

arr = [NSMutableArray arrayWithObjects:
            [NSMutableArray array], 
            [NSMutableArray array],    
            [NSMutableArray array],
            [NSMutableArray array],     
            [NSMutableArray array], 
            [NSMutableArray array],     
            [NSMutableArray array], nil];

Note that you must retain / release arr if you want to hold onto it. If what you really want is simply arrays of arrays, I have a class called RJGrid which will do this for you, and is faster than multiple NSMutableArrays (linked lists are slow for lookups).

You can download the class here, in my dropbox. It uses ARC, but is simple enough to convert to a reference counted environment, if you want me to do that.

Sign up to request clarification or add additional context in comments.

Comments

1

If you’re using ARC, you don’t need to do anything. If you aren’t using ARC, then you can autorelease the arrays:

arr = [[NSMutableArray alloc] initWithObjects:
                [[[NSMutableArray alloc] init] autorelease], 
                [[[NSMutableArray alloc] init] autorelease],    
                [[[NSMutableArray alloc] init] autorelease],
                [[[NSMutableArray alloc] init] autorelease],     
                [[[NSMutableArray alloc] init] autorelease], 
                [[[NSMutableArray alloc] init] autorelease],     
                [[[NSMutableArray alloc] init] autorelease], nil];

4 Comments

[NSMutableArray array] is about half as many keystrokes, and easier to read/understand.
Is there some way to use an NSAutorelasePool to simplify it? (No idea... I'm a Objective-C newb with about 10 days of experience).
@JasonDown no, you'd need to have ARC enabled. I honestly would recommend you learn manual reference counting first, however.
ARC is where the reference counting is handled for you right? (Gives you compile errors if you try to use [release xxx] and things like [super dealloc])
1

Use an autorelease memory pool and release the pool when you are done with your arr variable.

http://www.alphero.com/mobile-development/understanding-objective-c-autorelease-memory-management/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.