user1232840
user1232840

Reputation: 3

IOS adding and removing a subview memory leak

I have tried for a few days now to figure this out but i feel that i have gone about it the wrong way or am just missing something very simple.

I have a single view which takes a swipe gesture, upon the swipe, it will try to create a subview from a nib file. I am able to load the subview and remove the subview but there is a memory leak somewhere having to do with allocating the view or removing it.

In the current view .m file i am synthesizing my second view and in the .h file of the first view i also have a: @class settingsviewcontroller

settingsviewcontroller *settingsview

and then @property nonatomic retain for the settingsview

In the code below i am allocating the subview. It works without the [settingsview release[ but with that i get a bad memory error.

In the second part of the code, it is a function from the settingsviewcontroller to remove the view. It takes an action from the user and animates the view offscreen, and attempts to remove it, or at least i think it tries too.

- (IBAction)swiping:(id)sender {
  //SettingsViewController *SettingsView = [[SettingsViewController alloc] init];

  settingsView = [[SettingsViewController alloc] init];

  CATransition *animation = [CATransition animation];
  [animation setDuration:0.5f];
  [animation setType:kCATransitionPush];
  [animation setSubtype:kCATransitionFromTop];
  [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

  [self.view addSubview:[settingsView view]];
  [[self.view layer] addAnimation:animation forKey:@"SwitchToView1"];

  [settingsView release];
}



- (IBAction)rollOut:(id)sender {
  UIView *currentView = self.view;
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDidStopSelector:@selector(removeFromSuperview)];
  //[UIView setAnimationDidStopSelector:@selector(release)];
  [UIView setAnimationDuration:0.5f];
  currentView.transform = CGAffineTransformMakeTranslation(currentView.frame.origin.x, 480);
  [UIView commitAnimations];
}

Please give me some direction in this, i can understand how to do it if you just point me in the right direction.

Upvotes: 0

Views: 1503

Answers (1)

carl
carl

Reputation: 318

[self.view addSubview:[settingsView view]];

this code doesn't retain your settingsView,but "settingsview's view",so if you call [settingsView release]; the settingsView was released,if you call settingsView somewhere,you will got bad memory error.

this "@property nonatomic retain for the settingsview", so you can replace [settingsView release]; by self.settingsView = nil;

Upvotes: 4

Related Questions