views:

71

answers:

4

Hi Everyone,

I've got a fairly simple app that has the following in the view the program is mostly in:

int currentPageIndex;
NSArray *images;
NSString *nextImage;    
IBOutlet UIImageView *firstPage;
IBOutlet UIButton *bigButton;   

In the implementation viewDidLoad, I load the array with a bunch of image file names:

images = [NSArray arrayWithObjects:@"image1.jpg", @"image2.jpg", etc, nil];
[images retain];

Each time the bigButton is tapped, the image changes:

- (IBAction)bigButtonTapped:(id)sender {
    currentPageIndex++;

    nextImage = [images objectAtIndex:currentPageIndex];
    firstPage.image = [UIImage imageNamed:nextImage];
}

Everything works as I want it to, except that I am getting a "Received memory warning. Level=1" in the console with my device plugged in. This warning comes up after every 12 images or so, and eventually it crashes with "EXC_BAD_ACCESS"

I thought this would actually be a good way not to put anything in memory, as there is only one UIImageView on the screen and its image is changed as I need it to be.

It is a very simple app so I'm sure the fix is very simple... any ideas what I might be overlooking? Thanks so much!!

A: 

I assume Image is a retained property. Try to release it at the beginning of your bigButtonTapped.

Hope it helps.

Idan
This is bad advice. Just "trying releasing" is a quick way to add even more confusion to a memory management problem.
Jerry Jones
By trying I meant he should look for the documentation , and figure out if that's the right thing to do.Anyway, here it says that image is indeed retained : http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImageView_Class/Reference/Reference.htmlMeaning - he should release it.
Idan
A: 

Could be that you've reached the end of the array and you're trying to access past the end of the array. You could do a

currentPageIndex++;

if ( currentPageIndex < [images count]) {
nextImage = [images objectAtIndex:currentPageIndex];
firstPage.image = [UIImage imageNamed:nextImage];
}

Also could be that the image you listed doesn't exist in the bundle.

Henry Balanon
+1  A: 

Since you get a memory warning, the problem must be that the images aren't released. However, in the code you show, you're handling the images correctly. So the problem is most likely in a part of the code you're not showing us.

The only minor problem is see, which has been mentioned before, is that the currentPageIndex will eventually point outside of the range of the array. But this will cause a different error.

Codo
Codo, you are awarded the correct answer because there does indeed seem to be nothing wrong with the images. The memory warning is a part of some other code elsewhere. SORRY for a non-solvable question/post and THANK YOU for your response (and everyone else that responded).
ObjectiveFlash
A: 

There isn't really enough information here to say for sure what your problem is. EXC_BAD_ACCESS generally happens when you try to access an objects that has already been deallocated.

The quickest way to track down the real cause of EXC_BAD_ACCESS is by using the NSZombieEnabled executable argument, and then setting a breakpoint on objc_exception_throw. This will get you a stack trace, and allow you to determine specifically which object you are trying to access.

http://www.cocoadev.com/index.pl?NSZombieEnabled

Using Malloc to debug

Jerry Jones