1

I am developing an IOS application. How can I get the object from NSArray list. What is the best way to avoid getting memory error? Can you tell me the correct method? Thanks

Recommendation 1

Person *person = [[[Person alloc] init] autorelease];
        person = [self.userFavourites objectAtIndex:0];

Recommendation 2

Person *person = [self.userFavourites objectAtIndex:0];
[person retain];

//Make the required action

[person release];

Recommendation 3

?

3
  • 5
    Recommendation 3: start using ARC Commented Nov 20, 2013 at 10:16
  • 2
    why you are not using ARC i.e. automatic reference counting? Commented Nov 20, 2013 at 10:16
  • I dont wat to use ARC now :/ Commented Nov 20, 2013 at 10:18

3 Answers 3

5
Person *person = [self.userFavourites objectAtIndex:0];

the userFavourites array retains all elements inside and when you get an element it comes autoreleased.

EDIT:
Recommendation 1 - makes no sense to alloc init autorelease a Person object and then get different person from the array.
Recommendation 2 - you don't need to retain the object, as the array retains it. You only need to retain it if you need it outside your scope

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

2 Comments

Although recommendation 2 must be correct; he could call code that modifies that array and removes the object he's using.
And yet, he would have an autoreleased object until the end of the scope. Autoreleased objects are not like the weak references and they would not nullify once the array looses the strong pointer to the object. So in recommendation 2 retain/release on this particular pointer is not needed.
2

Recommendation 3:

Use ARC and modern objective-c so you write less code:

Person *person = self.userFavourites[0];

2 Comments

The OP has stated he doesn't want to use ARC and he's not legally obliged to do so.
He can use modern objective-c syntax though.
1

Why don't you use ARC? It makes your life easier...

However, I would go with recommendation 1, as the autorelease knows how to handle the objects, and it shouldn't give a memory error.

Any more help?

5 Comments

So go with the option that allocates a new object and then immediately reassigns the pointer to the object with another object?
If I use recommendation 1 then XCode analyzer show this message "Value stored to 'person' during initialization is never read"
Because you didn't continue your code, where you actually use your object.. Just continue and hopefully it disappear... Did it? :)
@trojanfoe is right. alloc init creates a new object. Then you assign another object to the same pointer... no sense at all.
@Mr_bem Actually I use this object next line NSString *queryParams=[NSString stringWithFormat:@"params=%@&",[Helper getFPersonJson:person]];

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.