PDA

View Full Version : Memory Leak issue




moomy
Jul 16, 2010, 08:32 AM
hi guys, I've written a little programme with a couple of UIImage animations and a random string grab from a plist. Been trying to put it on the iphone but it says there are issues... so i put it through a leak programme and it said there were memory leaks.

tried releasing more things but it crashes. Any help would be fantastic.
thanks



@implementation MainViewController
@synthesize quoteLabel;




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
}
return self;
[nibNameOrNil release];
}

- (void)viewDidLoad {


UIImageView* fireView = [[UIImageView alloc] initWithFrame:self.view.frame];


fireView.animationImages = [NSArray arrayWithObjects:

[UIImage imageNamed:@"plainshroom.png"],
[UIImage imageNamed:@"plainshroom.png"],
[UIImage imageNamed:@"plainshroom.png"],
[UIImage imageNamed:@"plainshroom.png"],
[UIImage imageNamed:@"plainshroom.png"],
[UIImage imageNamed:@"plainshroom.png"],
[UIImage imageNamed:@"blinkshroom"],

nil];

fireireView.animationDuration = 14.00;
// repeat the annimation forever
fireView.animationRepeatCount = 0;
// start animating
[fireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];
[fireView release];


}


-(IBAction) shockedShroom{

UIImageView* shockedView = [[UIImageView alloc] initWithFrame:self.view.frame];

// load all the frames of our animation
shockedView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"star1.png"],
[UIImage imageNamed:@"star2.png"],
[UIImage imageNamed:@"star3.png"],
[UIImage imageNamed:@"star4.png"],
[UIImage imageNamed:@"star6.png"],
[UIImage imageNamed:@"star7.png"],
[UIImage imageNamed:@"star8.png"],




nil];



shockedView.animationDuration = 0.30;

shockedView.animationRepeatCount = 4;
// start animating
[shockedView startAnimating];
// add the animation view to the main window
[self.view addSubview:shockedView];
[shockedView release];
}







- (void)readPlist
{


NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"quotes.plist"];
NSArray *array1 = [[NSArray arrayWithContentsOfFile:finalPath]retain];



srandom(time(NULL));
int r = random() %140;

NSString *arrayData1 = [array1 objectAtIndex:r];

quoteLabel.text = arrayData1;


[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:quoteLabel cache:YES];
UIView.animationDuration = 2.00;

[UIView commitAnimations];





quoteLabel.text = arrayData1;

[arrayData1 release];





}






- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {

[self dismissModalViewControllerAnimated:YES];
}



- (IBAction)showInfo:(id)sender {

FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
controller.delegate = self;

controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];

[controller release];
}


- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)dealloc {
[quoteLabel release];


[super dealloc];
}


@end



AndyCodez
Jul 16, 2010, 08:34 AM
What did you try to release?

Could try to put that code in a autorelease pool

NSAutoreleasePool *pool = [[NSAutoReleasePool alloc] init];

// Your code

[pool release];

moomy
Jul 16, 2010, 10:28 AM
tried to release array1, and some strings.

will have a look at the NSAutoreleasePool, will that work for the iphone?? Do I need to do it for each method??

dejo
Jul 16, 2010, 10:30 AM
Have you tried a Build and Analyze?

moomy
Jul 16, 2010, 10:33 AM
the array with the plist contents- array1 is retained but when I try and release it it crashes, and if it's not retained it doesn't work.

moomy
Jul 16, 2010, 10:33 AM
thanks dejo, will try it now.

moomy
Jul 16, 2010, 10:38 AM
just did it and it said

quoteLabel.text = arrayData1; ------ Potential leak of an object allocated on line 123.... this is the alloc of array1, the array with contents of file.... must be the retain problem. Not sure how I can release it. .. will try Autorelease pool? just for the one method???

dejo
Jul 16, 2010, 10:48 AM
NSArray's arrayWithContentsOfFile: already returns an autoreleased object, I believe. Why do anything with it? That is, no need to retain it, no need to release it.

EDIT: P.S. Why are you releasing arrayData1; you never alloc'd it.

robbieduncan
Jul 16, 2010, 11:07 AM
EDIT: P.S. Why are you releasing arrayData1; you never alloc'd it.

He releases nibNameOrNil in the init method without ever allocing it either...

moomy
Jul 16, 2010, 11:19 AM
lol, yeah, when i found i had memory problems i just tries releasing other stuff in case it helped. I'm a noob really so strugglilng a bit. :)

robbieduncan
Jul 16, 2010, 11:22 AM
lol, yeah, when i found i had memory problems i just tries releasing other stuff in case it helped. I'm a noob really so strugglilng a bit. :)

Follow The Rules (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html) and everything will be fine. Read, comprehend and take in the entire document.

PhoneyDeveloper
Jul 16, 2010, 12:06 PM
He releases nibNameOrNil in the init method without ever allocing it either...

Actually he doesn't because he returns on the line before that ;-)

OP, you really can't come on here and say "here's my terrible code can you fix it for me". Read the memory management rules. Then write some code. They read the rules again. Then you can come here with specific questions. That's the only way you'll learn it. Also, get a book and read that.

dejo
Jul 16, 2010, 12:18 PM
lol, yeah, when i found i had memory problems i just tries releasing other stuff in case it helped. I'm a noob really so strugglilng a bit. :)
Well, you may get lucky with that approach occasionally but you'll find, as you are currently, that the "spaghetti-on-the-wall" technique usually doesn't work. And it's a bad habit to form. Especially when just starting out, it is important to understand the code you are writing before moving on. And when you run into problems, learn how to debug errors and crashes and understand what you are doing when you fix them. Hope that helps.

moomy
Jul 16, 2010, 12:53 PM
thanks guys, will study the rules at length. I've also got things working... I removed the retain and the release commands that were not needed. :):)