See the following code, it is simply a method, and I am unable to understand the memory management issues in it, let me know, what is happening in the following code, and why?
-(IBAction)selectMarina {
NSLog(@"Select Marina");
actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
//[pickerView release];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Close"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
//[closeButton release];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
[pickerView selectRow:0 inComponent:0 animated:NO];
}
here it is saying Potential leak of object allocated on line 112 and stored into close button
it is showing such msg on line
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
How can I do proper memory management in this code, and can you specify it in details
Thanks
First of all you need to release both the instance pickerView and closeButton.
As per rule whenever u alloc an object it has to be released once it job is completed. In your case after adding both the instance to the subview are not required so they has to released. Otherwise it will show the leak as you mentioned.
For more understanding refer memory guide or for a quick look click http://www.raywenderlich.com/2657/memory-management-in-objective-c-tutorial