views:

144

answers:

1
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congratulations" message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"View", nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(110, 100, 80, 80)];
NSString *imagePath = [NSString stringWithFormat:@"%@", [Array objectAtIndex:x]];
UIImage *bkgImg = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagePath ofType:@"png"]];
imageView.image = bkgImg;
[bkgImg release];
[alert addSubview:imageView];
[imageView release];
[alert show];
[alert release];    

That is the code that I am using to create the alert view. Currently, I have it set up so if the user presses one of the buttons, it will load up a new viewcontroller. It worked fine until I added a subview to the UIAlertView. Now, whenever it animates to the new screen, it just crashes the program. I am fairly new to iPhone development and any help would be appreciated.

+1  A: 

You are doing:

UIImage *bkgImg = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagePath ofType:@"png"]];
...
[bkgImg release];

But +imageWithContentsOfFile returns an autoreleased UIImage instance, so you should not release it yourself. What probably is happening is the NSAutoreleasePool sending a -release to an object that has already been deallocated, causing the app to crash later on.

I would recommend taking a good look at http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html (or equivalent iPhone docs if those exist).

xnyhps