Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

NexesDev

macrumors newbie
Original poster
May 12, 2008
14
0
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
 
imageArray is defined like this

Code:
NSMutableArray *imageArray;

with no property decleration. I was playing with them but didn't seem to help
 
Is the 'takeBite' method in the same class in which 'imageArray' is declared? Do you have an '[imageArray release];' anywhere in your code?
 
Is the 'takeBite' method in the same class in which 'imageArray' is declared? Do you have an '[imageArray release];' anywhere in your code?

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
 
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.
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.
 
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.
 
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.
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:
 
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:

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.
 
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:

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.
 
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.
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?
 
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?

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.