I am trying to change the animation of objects based on their present state. I had this working fine, but then I used the instruments tool and noticed that I had all kinds of memory leaks. I have tried to fix this by two ways, but they cause a crash, can anyone help me figure this out?
So here is a set up function I used to assign the animation:
and I would just call [myCharacter setup"right"]; for example to change the animation, which would work fine, but I think because every time I call setup I am reallocating "sprite" and the animations, it causes a leak.
so my next step was to change the setup function so that it just allocates that stuff, so it would look like this:
and then I would have another function called update that would reassign the animations, without having to reallocate anything like this:
but for some reason, when I try this, it causes a crash...
so the last thing I tried was to store things that changed in temp variables, assign them to the things I retain, and then release the temp variables like this:
if I have the [a b c d release] thing above, that causes a crash, if I don't release them, I still have the memory leak. Anyone know how I can fix this, what I am doing wrong? Or should I not really worry about the memory leak?
So here is a set up function I used to assign the animation:
Code:
- (void) setup: (NSString*) animation {
// assign the image
sprite = [[UIImageView alloc] initWithFrame:CGRectMake(100.0, 400.0, 75.0, 75.0)] ;
animationCenter = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4" ofType:@"gif"]],
nil];
animationLeft = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Left" ofType:@"gif"]],
nil];
animationRight = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Right" ofType:@"gif"]],
nil];
animationCrouch = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Crouch" ofType:@"gif"]],
nil];
if(animation == @"center") {
sprite.animationImages = animationCenter;
}
else if(animation == @"left") {
sprite.animationImages = animationLeft;
}
else if(animation == @"right") {
sprite.animationImages = animationRight;
}
else if(animation == @"crouch") {
sprite.animationImages = animationCrouch;
}
[self addSubview:sprite];
}
and I would just call [myCharacter setup"right"]; for example to change the animation, which would work fine, but I think because every time I call setup I am reallocating "sprite" and the animations, it causes a leak.
so my next step was to change the setup function so that it just allocates that stuff, so it would look like this:
Code:
- (void) setup {
// assign the image
sprite = [[UIImageView alloc] initWithFrame:CGRectMake(100.0, 400.0, 75.0, 75.0)] ;
animationCenter = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4" ofType:@"gif"]],
nil];
animationLeft = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Left" ofType:@"gif"]],
nil];
animationRight = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Right" ofType:@"gif"]],
nil];
animationCrouch = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Crouch" ofType:@"gif"]],
nil];
}
and then I would have another function called update that would reassign the animations, without having to reallocate anything like this:
Code:
-(void) update:(NSString*) animation {
if(animation == @"center") {
sprite.animationImages = self.animationCenter;
}
else if(animation == @"left") {
sprite.animationImages = self.animationLeft;
}
else if(animation == @"right") {
sprite.animationImages = self.animationRight;
}
else if(animation == @"crouch") {
sprite.animationImages = self.animationCrouch;
}
[self addSubview:sprite];
}
but for some reason, when I try this, it causes a crash...
so the last thing I tried was to store things that changed in temp variables, assign them to the things I retain, and then release the temp variables like this:
Code:
- (void) setup: (NSString*) animation {
// assign the image
NSArray *a,*b,*c,*d;
temp = [[UIImageView alloc] initWithFrame:CGRectMake(100.0, 400.0, 75.0, 75.0)] ;
a = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4" ofType:@"gif"]],
nil];
b = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Left" ofType:@"gif"]],
nil];
c = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Right" ofType:@"gif"]],
nil];
d = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ship_4_Crouch" ofType:@"gif"]],
nil];
self.animationCenter = a;
self.animationLeft = b;
self.animationRight = c;
self.animationCrouch = d;
if(animation == @"center") {
temp.animationImages = animationCenter;
}
else if(animation == @"left") {
temp.animationImages = animationLeft;
}
else if(animation == @"right") {
temp.animationImages = animationRight;
}
else if(animation == @"crouch") {
temp.animationImages = animationCrouch;
}
xPos = 50.0;
yPos = 230.0;
self.sprite = temp;
[temp release];
[a release];
[b release];
[c release];
[d release];
[self addSubview:sprite];
}
if I have the [a b c d release] thing above, that causes a crash, if I don't release them, I still have the memory leak. Anyone know how I can fix this, what I am doing wrong? Or should I not really worry about the memory leak?