NSMutableArray problems ARG

Discussion in 'iOS Programming' started by NexesDev, Jun 17, 2009.

  1. NexesDev macrumors newbie

    Joined:
    May 12, 2008
    #1
    in my project i have 9 pictures. i load each picture into an nsmutablearray so that when another method is called, a new image is displayed.

    here is the method that populates the nsmutablearray

    Code:
    - (void)viewDidLoad 
    {
    	[super viewDidLoad];
    
    	foodImage.image = [UIImage imageNamed:@"hamburger.jpg"];
    	imageArray = [[NSMutableArray  alloc] init];
    	
    	for(int i = 1; i < 10; i++) {
    		UIImage *image = [UIImage imageNamed:[NSString stringWithFormat: @"bite-%d.jpg", i]];
    		[imageArray addObject:image];
    		[image release];
    	}
    	NSLog(@"array count %d", [imageArray count]);
    	NSLog(@"array retain count: %d", [imageArray retainCount]);
    
    }
    
    this loads the array correctly, the array count is 9 which is correct and the retain count is 1, also correct.

    here is the method that access the images in the array

    Code:
    -(void)takeBite:(int)biteNumber
    {
            NSLog(@"array retain count: %d", [imageArray retainCount]);
    	UIImage *tempPic = [imageArray objectAtIndex: biteNumber];
    	if (tempPic)
    	 {
    		 NSLog(@"if statement true");
    		 foodImage.image = tempPic;
    	 }
    }
    
    but now, and i don't know why, the retain count is 0, the if statement always returns false (obviously if retain count is 0) and the image doesn't change.

    could someone tell me what is going on here, why is my array's retain count 0? I cant use any images in the array.

    thanks for your help
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  3. NexesDev thread starter macrumors newbie

    Joined:
    May 12, 2008
    #3
    imageArray is defined like this

    Code:
    NSMutableArray *imageArray;
    
    with no property decleration. I was playing with them but didn't seem to help
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    And this is declared in the .h?
     
  5. NexesDev thread starter macrumors newbie

    Joined:
    May 12, 2008
  6. kalimba macrumors regular

    Joined:
    Jun 10, 2008
    #6
    Is the 'takeBite' method in the same class in which 'imageArray' is declared? Do you have an '[imageArray release];' anywhere in your code?
     
  7. NexesDev thread starter macrumors newbie

    Joined:
    May 12, 2008
    #7
    yes the method "takeBite" is declared in my ViewController.m class, the same class that my imageArray is declared in. in my uiview subclass in the method "touchesDidEnd" when the user taps twice it calls the "takeBite" method in order to change the displaying picture. the call from my uiview class to the uiviewcontroller class works.

    i call [imageArray release]; in my dealloc method in the uiviewcontroller class, the same class imageArray is declared in.

    im not a newbie programmer, but for some reason this problems is eluding me and its driving me nuts. thanks
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Is the instance of the viewController that you call the takeBite method on the same instance of the viewController that you assigned imageArray for?

    Anyways, why is your touchesDidEnd in your UIView subclass and not in your UIViewController? That's where it normally resides.
     
  9. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    Code:
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat: @"bite-%d.jpg", i]];
    [imageArray addObject:image];
    [image release];
    You're over-releasing image here because imageNamed: returns an autoreleased object, so you shouldn't release it again unless you retain it. Remove the [image release]; line.
     
  10. kalimba macrumors regular

    Joined:
    Jun 10, 2008
    #10
    I believe you're correct and that issue should be fixed, but... how would that be causing the 'imageArray' retain count to drop to zero? :confused:
     
  11. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #11
    because:
    you create the image, retain count 1. then you add it to the array, retain count 2. then you release it, retain count 1. later, it is autoreleased - retain count 0.

    you should not use autorelease in this case. it's better practice to release it at the end of the loop yourself instantly rather than piling up several images in the memory that get autoreleased later.
     
  12. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    If retainCount is returning 0, then most likely the object it's being called on is nil. A method that returns a number will return 0 if called on a nil object. If the retain count was truly 0, then the object would already be deallocated and the app would crash at that point.
     
  13. kalimba macrumors regular

    Joined:
    Jun 10, 2008
    #13
    Yeah, I understand that part. But it's the array elements that are being over-released, not the array (imageArray) itself. I get how over-releasing the elements would cause problems with the elements, but how should that affect the retain count of the array container?
     
  14. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #14
    maybe the array gets released when it is empty? just a wild guess, I can't check it right now. though that would be a strange behaviour.
     

Share This Page