2

can anyone clear this up for me ?
I am building an iPad App that has a TableViewController that is supposed to show something between 1000 and 2000 strings. I have those NSStrings in a Singleton.
In the init Method of the Singleton I initialize an Array that holds all the data ( does not have to be the final way to do it - was just a quick copy and paste for testing )
I did an self.someArray = [[NSArray alloc]initWithObjects: followed by the large number of strings, followed by nil.
that worked fine in the simulator - but crashed with bad access on the iPad right on Application startup
If I use the convenience method [NSArray arrayWithObjects:instead - it works fine.
I looked into Instruments and the overall memory footprint of the App is just about 2,5 MB.
Now I don't know why it works the one way but not the other.
EDIT:

#import "StaticValueContainer.h"`

static StaticValueContainer* instance = nil;
@implementation StaticValueContainer
@synthesize customerRatingKeys;

+(StaticValueContainer*)sharedInstance
{
    if (instance == nil){
        instance = [[StaticValueContainer alloc]init];
    }
    return instance;
}

-(id)init
{
    if  ( ( self = [super init] ))
    {
        [self initCustomerRatingKeys];

    }
    return self;
}
-(void)init customerRatingKeys
{
 self.customerRatingKeys = [[NSArray alloc]initWithObjects:
 @"string1",
....
 @"string1245"

,nil
}

as I said: it crashes on the device with self.customerRatingKeys = [[NSArray alloc]initWithObjects: but works with *self.customerRatingKeys = [[NSArray arrayWithObjects...`

1 Answer 1

7

Well, there isn't much difference between them: arrayWithObjects returns an auto-released array that you don't need to release yourself (unless you subsequently retain it), and initWithObjects returns an array you must then release to avoid a memory leak. Performance wise there is no difference between them.

I would suggest if you're getting a bad access error using initWithObjects but not with arrayWithObjects there might be some sort of memory management error in your code. If you post the code itself you'll probably get a more exact response.

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

5 Comments

yes - this is the difference I know about... But i cant figure out how this is related to this crash. Maybe there is something wrong from a memory perspective, I added some code, maybe that helps to clear things up
self.customerRatingKeys = [[NSArray alloc]initWithObjects: - you shouldn't be doing this, assuming your property is declared as retain (which is typically would be). How are you declaring your property?
Yes I declare it with retain. What problem am I missing ?
Because if you've declared your property as retain you're retaining your array twice, which is going to cause a memory leak. You should add an 'autorelease' as follows: self.customerRatingsKeys = [[[NSArray alloc] initWithObjects:...]autorelease]
Right. Understand that. That is why I used the arrayWithObjects method in the first place. I thought that would create an autoreleased object ?

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.