Memory Leak issue

Discussion in 'iPhone/iPad Programming' started by moomy, Jul 16, 2010.

  1. macrumors newbie

    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.

    @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"],
    		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"],
    	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];
    	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];
  2. macrumors regular

    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];
  3. macrumors newbie

    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??
  4. Moderator


    Staff Member

    Have you tried a Build and Analyze?
  5. macrumors newbie

    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.
  6. macrumors newbie

    thanks dejo, will try it now.
  7. macrumors newbie

    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???
  8. Moderator


    Staff Member

    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.
  9. Moderator


    Staff Member

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

    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. :)
  11. Moderator


    Staff Member

    Follow The Rules and everything will be fine. Read, comprehend and take in the entire document.
  12. macrumors 68030


    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.
  13. Moderator


    Staff Member

    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.
  14. macrumors newbie

    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. :):)

Share This Page