It’s a simple app that takes 30 pics from my Resources folder, puts them into an NSMutableArray during viewDidLoad, and then changes the pic based on accelerometer data. I have a UIImageView from IB covering the whole view, and when the accelerometer updates its info, it swaps the uiimageview.image to a diff pic. Works for about 10 seconds, and then crashes with this:
Program received signal: “0”.
Data Formatters temporarily unavailable, will re-try after a 'continue'. (Unknown error loading shared library "/Developer/usr/lib/libXcodeDebuggerSupport.dylib")
After looking around on here it sounds like it’s a memory leak. Here’s the code I’m using:
Interface:
{
UIAccelerometer *accel;
IBOutlet UIImageView *pic;
NSMutableArray *picArr;
}
Implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
picArr = [[NSMutableArray alloc] init]; //array of pics to be used
NSString *path = [[NSString alloc] init]; //dynamic path of images to save to array
for (int i = 1; i <= 30; i++)
{
path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"nativity_test_%i", i] ofType:@"jpg"];
[picArr addObject:[[UIImage alloc] initWithContentsOfFile:path]];
}
accel = [UIAccelerometer sharedAccelerometer];
accel.delegate = self;
accel.updateInterval = 0.5f;
[path release];
pic.image = [picArr objectAtIndex:15];
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
if(acceleration.y < 0.0f) //goes thru the tilted left pics
{
for (int i = 0; i <= 15; i++)
{
if( acceleration.y >= (-(15.0f-(double)i)/30.0f) && acceleration.y < (-(14.0f-(double)i)/30.0f) )
{
pic.image = [picArr objectAtIndex:i+1];
}
}
}
else if(acceleration.y > 0.0f) //goes thru tilted right pics
{
for (int i = 1; i <= 14; i++)
if( acceleration.y > ((double)i/30.0f) && acceleration.y <= ((double)(i+1)/30.0f) )
{
pic.image = [picArr objectAtIndex:i+15];
}
}
}
Please let me know if I left out any pertinent info.
This is not a crash caused by a memory leak, but by an overrelease of memory. Your crash is caused by this line:
Remove that line and you should be fine. For some values of fine, anyway; most of what George says also applies.
The reason is that the path being released will be this one:
That’s an object with an effective retain count of zero. While it has a positive retain count, it’s already been autoreleased and will be released when you return to the main event loop.
You should also remove this line:
The reason is you’re assigning the result of pathForResource to path, never actually using it. You’ll need to declare path as a variable, of course. I suggest changing the pathForResource line to this:
This is a smart change, as there’s no reason you’d need this outside of the loop.
You should also fix this line:
[UIImage alloc] returns an UIImage with an effective retain count of 1. Adding it to an array will increase that to two. If you’re just releasing the array later, this will cause a memory leak. Instead:
That will mean you’re adding an UIImage with an effective retain count of 0 to the array, which will bump it to 1, making the array the only owner of the object. Later when the array goes away, so will most of the images. (The imageView will also own whichever is assigned to it.)
Don’t be frustrated by this stuff: one day soon you’ll just suddenly get it and wonder why it ever caused you headaches. 🙂