1

I am not sure if I am writing this code correctly.

Fruit * o1 = [[Fruit alloc] initWithName:kFruitOrange imageView:orange1] ;
fruitArray = [[NSMutableArray arrayWithObjects:o1, nil] retain]; // retain array for later use.

There are actually several fruits. Since I alloc them, then assign them to an array in my class init method. When and how should I release these objects.

CLARIFICATION. If I release [o1 release] after saving in array, I get "exc_bad_access" errors when looking at the array in other methods.

In my viewDidUnload method, I have gone through the array and released each object manually.

  for (Fruit * f  in fruitArray) {
        [f release];
    }

I have never seen anyone else do this in code before, so I am thinking that this is not the correct way to do it?

UPDATE

-(Fruit *) initWithName:(enum fruitTypes)fruitName imageView:(UIImageView *)iv{
    if((self = [super init])){
        name = @"Fuit Object";
        NSLog(@"creating orange colour %i", fruitName);
        switch (fruitName) {
            case kFruitOrange:
                 NSLog(@"creating orange colour ");
                colour = [UIColor orangeColor] ;
                break;
            case kFruitBanana:
                colour = [UIColor yellowColor];
                break;
            case kFruitKiwi:
                colour = [UIColor greenColor];
                break;
            case kFruitBlue:
                colour = [UIColor blueColor];
                break;
            default:
                NSLog(@"COLOUR NOT FOUND");
                break;
        }
        value = fruitName;
        imageView = iv;
        center = iv.center;


        [colour retain];
        [imageView retain];
        [name retain];


        return self;
    }
    return nil;

}

** ANSWER WAS I NEEDED TO RETAIN THE VARIABLES I SET IN MY FRUIT.M CLASS.

3
  • Take that for() loop out of your viewDidUnload method. It is absolutely wrong to manage your memory like that. Commented May 2, 2011 at 23:34
  • Paste the code for -[Fruit initWithName:imageView:]. Commented May 2, 2011 at 23:36
  • IT LOOKS TO BE SOMEWHERE ELSE IN MY CODE ; ( THANKS VLADIMIR, CORRECT ANSWER BELOW. Commented May 2, 2011 at 23:52

1 Answer 1

6

Release objects right after you add them to array - standard objective-c containers retain their elements and then release them when container itself is deallocated, so you don't need to worry about memory management yourself.

So when you create and fill your array release your elements:

Fruit * o1 = [[Fruit alloc] initWithName:kFruitOrange imageView:orange1] ;
fruitArray = [[NSMutableArray arrayWithObjects:o1, nil] retain];
[o1 release];

And in your viewDidUnload method (and in dealloc) just release your array, not its elements:

[fruitArray release];

The reason you get errors if you put [o1 release]; in your code is that in that case you release your object twice and it have been retain just once - when allocated.

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

5 Comments

I think the poster said that if he released it right after, he gets errors later in the code.
@Kal yep, but that's an indication that he's got errors elsewhere in his code. Vladimir's answer is correct.
Hi Vladimir. I thought that was correct as well, and I still think that is correct. But when I release my Fruit object after adding it into the array, the fruit object does not seemed to be retained so when I access it later it causes "exc_bad_access". This is why I am a bit confused and added the unique release loop.
Does your actual code differ from what you posted? May be if do something different with multiple fruits... This approach should work fine
You are correct. The issue is in my Fruit Class, When I do release after adding to array. I cannot access fruit.colour, I save this value in my Fruit.m file with colour = [[UIColour redColor] retain]; But now I cannot access fruitObject.colour later in code? I don't quite get why my colour object is not retained?

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.