Memory management, release needed? - iphone

I wonder if someone could quickly help me with the following, do I need to add a [myTableView release]; after I call [view addSubview:[self myTableView]]; ? Initially I was thinking no, and running it through CLANG produced to memory warnings
Here is my thinking:
[self setMyTableView:tempTableView]; retainCount = (+1)
[view addSubview:[self myTableView]]; retainCount = (+2)
//[myTableView release]; << HERE retainCount = (+1)
-dealloc [myTableView release]; retainCount = ( 0)
.
#property (nonatomic, retain) UITableView *myTableView;
.
- (void)loadView {
NSLog(#"%s", __PRETTY_FUNCTION__);
[self setTitle:#"Location Data"];
CGRect viewFrame = CGRectMake(0, 20, 320, 460);
UIView *view = [[UIView alloc] initWithFrame:viewFrame];
CGRect tableFrame = CGRectMake(0, 0, 320, 416);
UITableView *tempTableView = [[UITableView alloc] initWithFrame:tableFrame];
[self setMyTableView:tempTableView];
[tempTableView release];
[view addSubview:[self myTableView]];
//[myTableView release]; << HERE
[[self myTableView] setDelegate:self];
[[self myTableView] setDataSource:self];
[self setView:view];
[view release];
}
.
- (void)dealloc {
[myTableView release];
[dataModel release];
[super dealloc];
}
EDIT: hmm maybe I don't as [view addSubview:[self myTableView]]; retains it and will release when done. Your right Carl, my bad. I was getting confused with: alloc, set, release, when this is simply the view taking ownership (and the responsibility to release that later)

You're right when you say that [view addSubview:[self myTableView]]; will retain your table view, but since it's the view who is retaining it, it's the view that is supposed to release it. And it will, when the view gets dealloc'ed. You only have to release what you retained yourself, i.e., you only have to release the table view once in your dealloc method.

Your method doesn't retain myTableView, so it shouldn't release it.

Related

While releasing the UIView Object my app is crashing

I developed an iPhone project without XIB files. In that I manually created the UIView object and a few more components on that view also. I release these objects in my dealloc and viewDidUnload methods. My problem is that if I release the UIView object the app crashes and I do not know why. Can anyone help me in this issue?
Here is my code:
allmessengersview = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
allmessengersview.backgroundColor = [UIColor whiteColor];
self.view = allmessengersview;
-(void) dealloc {
[allmessengersview release];
[super dealloc];
}
- (void) viewDidUnload {
[super viewDidUnload];
[allmessengersview release];
allmessengersview = nil;
}
Please Write Down this Code in Initialization in View
allmessengersview=[[UIView alloc]init];
allmessengersview.frame = CGRect (0,0,320,480);
allmessengersview.backgroundColor = [UIColor whiteColor];
[self.view addsubView: allmessengersview];
Please Write Down this Code in Dealloc Methods
-(void)dealloc
{
[allmessengersview release];
[super dealloc];
}
Don't release in viewDidUnload.
This way the app will release allmessengersview in the viewDidUnload and later on it tries to release it a second time in dealloc, but since it's already released, the app crashes.

memory leak on dealloc

I am new to iPhone Development.
I have taken UIView,UIImageView,UIScrollView etc in my NIB File.
My question is that can i remove all that views from SuperView ?
For Example:`
-(void)dealloc
{
[super dealloc];
[imageView removeFromSuperview];
[View1 removeFromSuperview];
[View2 removeFromSuperview];
[ScrollView removeFromSuperview];
[imageView release];
[View1 release];
[View2 release];
[ScrollView release];
imageView = nil;
View1 = nil;
View2 = nil;
ScrollView = nil;
}
please help me.Thanking you...
You need to call [super dealloc]; as the very last thing, not first. That's because [super dealloc] cleans up your object, and accessing instance variables afterwards is undefined behavior: anything can happen. For example, [super dealloc] could set all instance variables to nil, in which case the following release calls simply have no effect (and thus cause leaks). Also, there's no need to set the instance variables to nil since you're not going to use them anyway. So all you need is:
-(void)dealloc
{
[imageView release];
[View1 release];
[View2 release];
[ScrollView release];
[super dealloc];
}
A few more notes: the removeFromSuperview calls are harmless but unnecessary, this is implicitly done by [super dealloc]. And you should stick to Apples naming conventions which in your case means you shouldn't start variable names with uppercase letters. So it should be scrollView instead of ScrollView.
No need to remove those subviews from superview, since anyhow you are going to release the superview itself.
You can make the dealloc as follows.
-(void)dealloc{
[imageView release];
[View1 release];
[View2 release];
[ScrollView release];
imageView = nil;
View1 = nil;
View2 = nil;
ScrollView = nil;
[super dealloc];
}
Note that [super dealloc] is at the end (More info).
You might want to enable ARC in your projects. You won't need to release these objects anymore; the system will take care of (most of) your memory-management.
You can read more about ARC here: http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

iOS memory leaks causing crash in changing view controllers

I have created two view controllers in which there is UIImage animations.Its crashing frequently and showing memory leaks in xcode instrument.
My Controllers Code-
- (void)viewDidLoad {
NSArray *firstArray;
firstArray = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"up0001" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"up0002" ofType:#"png"]],
::
::
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"up0035" ofType:#"png"]], nil];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
imgView = UIViewContentModeScaleToFill;
[imgView setAnimationImages:firstArray];
imgView.animationDuration = 1.75;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
[self.view addSubview: imgView];
}
- (void)dealloc {
[super dealloc];
[imgView release];
imgView = nil;
}
And I am changing viewcontrollers as my rootviewcontroller by getting appdelegate object and calling following appdelegate function in my Appdelegate.m(Please suggest any good approach)
- (void)changeRootViewController:(NSString *)controllerName
{
if(self.viewController){
[self.viewController.view removeFromSuperview];
self.viewController=nil;
}
if (controllerName == #"ViewController") {
ViewController *lviewController =[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.viewController = (RootViewController *)lviewController;
[lviewController release];
lviewController.view = nil;
[self.window setRootViewController:self.viewController]; //LEAKS 100%
} else if (controllerName == #"MainViewController") {
// Use a different VC as roowViewController
MainViewController *lviewController =[[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.viewController = (RootViewController *)lviewController;
[lviewController release];
lviewController.view = nil;
[self.window setRootViewController:self.viewController]; //LEAKS 100%
} else if (controllerName == #"SecondViewController") {
// Use a different VC as roowViewController
SecondViewController *lviewController =[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.viewController = (RootViewController *)lviewController;
[lviewController release];
lviewController.view = nil;
[self.window setRootViewController:self.viewController]; //LEAKS 100%
}
[self.window makeKeyAndVisible];
}
And calling this in my respective controlers button pressed as -
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:#"ViewController"];
OR
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:#"MainViewController"];
I want to manage view controllers from my main controller swapping controllers in and out without Navigationbar.Please help me figure out the best possible approach and avoid leaks as well.
Look at this piece of code:
if (controllerName == #"ViewController") {
ViewController *lviewController =[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.viewController = (RootViewController *)lviewController;
[lviewController release];
lviewController.view = nil;
[self.window setRootViewController:self.viewController]; //LEAKS 100%
}
When you alloc "lviewController", its retain count is 1;
when you do "self.viewController" as I suppose viewController is a retained property, then lviewController retain count is increased to 2;
then you release it, balancing the previous alloc, and retain count returns to 1;
finally you assign it to rootViewController, which is a retained property, so again lviewController retain count is 2;
finally when you "swap" view controller in your window, the lviewController is released, so it retain count becomes 1. As you can see, it will never be dealloc'd. This means that each time you call this function, you make a leak.
You require to change some of your statement in your code ..
This line have no meaning ..
imgView = UIViewContentModeScaleToFill;
There is a function by using that you have to set the value of the UIImageView property to UIViewContentModeScaleToFill find that in a UIImageView documentation.
[imageView setContentMode:UIViewContentModeScaleToFill];
The same with this...
lviewController.view = nil; //Remove this from your code ..
And finally change the implmentation for the dealloc function and remember [super dealloc]; should be in the last in any implementation of dealloc .
- (void)dealloc {
[imgView release];
imgView = nil;
[super dealloc];
}
The probable leak is due to the code inside "SecondViewController", "MainViewController", "ViewController" classes. And the reason for crash is you are releasing image view after [super dealloc]
try this,
- (void)dealloc {
[imgView release];
imgView = nil;
[super dealloc];
}

Autorelease causing app crash

I've been spinning on this for 2 hours now, so thought I'd post here for some advice.
In my viewDidLoad method, I do:
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
...
UIView *contactsRow = [self generateContactsRow];
contactsRow.frame = CGRectMake(10, tableMaxY, 300, 56);
...
[scrollView addSubview:contactsRow];
[self.view addSubview:scrollView];
[scrollView release];
In [self generateContactsRow], I basically create a container view, load a bunch of other views (button, label, etc) inside, and return it.
UIView *containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 0)] autorelease];
... (define stuff)
// set it
[containerView addSubview:imageView];
[containerView addSubview:textLabel];
[containerView addSubview:detailTextLabel];
[containerView addSubview:addButton];
[containerView addSubview:hr];
// and forget it
[imageView release];
[textLabel release];
[detailTextLabel release];
[addButton release];
[hr release];
return containerView;
The fact that I'm autoreleasing containerView is causing my app to crash. I return an alloc'd object, so I thought I had to release it somehow, and autorelease seemed the best way. If I remove it, things work fine, but won't I then have a memory leak?
Thanks
Make the segment like the following
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
...
UIView *contactsRow = [[self generateContactsRow] retain];
contactsRow.frame = CGRectMake(10, tableMaxY, 300, 56);
...
[scrollView addSubview:contactsRow];
[self.view addSubview:scrollView];
[contactsRow release];
[scrollView release];
I am retaining the contactsRow here and releasing that after I have it as subview to the scrollView. So there won't be any memory leak.
You can release it in viewdidunload or viewwillDisapper methods...

UIImageViews added to UIScollView never get deallocated, Memory Leak?

This is my first post and you are my last hope.
I have a list with images in my iPad app, if you select one, my class MyViewController which extends UIViewController is loaded and shown (by just setting window.rootViewController, by using UINavigationController, by presentModalViewController - tried everything, doesn't make any difference). It has a UIScrollView inside, which adds a big version of the image to the UIScrollView with this code:
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[[delegate getImageNames] objectAtIndex:(gameNr*2)]]];
tempImageView.frame = CGRectMake(0,0,tempImageView.frame.size.width/2,tempImageView.frame.size.height/2);
[self setMyImage:tempImageView];
[tempImageView release];
myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
myScrollView.maximumZoomScale = 2.0;
myScrollView.minimumZoomScale = 1.0;
myScrollView.clipsToBounds = YES;
myScrollView.bounces = NO;
myScrollView.delegate = self;
[myScrollView addSubview: myImage];
myImage is a (nonatomic, retain) property of the type UIImageView inside my MyViewController.
After pressing a button and go back to the list of images, I release the reference to MyViewController and it's dealloc method is called like this:
- (void)dealloc
{
CFShow(#"dealloc!");
for(UIImageView *subview in [myScrollView subviews]) {
if(subview.frame.size.width > 20){
[subview removeFromSuperview];
[subview setImage:nil];
}
}
[myImage setImage:nil];
self.myScrollView = nil;
self.myImage = nil;
[myScrollView release];
[myImage release];
[super dealloc];
}
It works so far, but the problem is, that the memory of the images is not really deallocated this way. After opening and closing about 12 images, the app crashes with memory warning.
This is also confirmed by the report_memory method I got from here:
iOS Low Memory Crash, but very low memory usage
The dealloc method IS called, that's triple checked.
I checked the variables myImage and myScrollViewvia breakpoint in the debugger, they are set to 0x0 by those commands in the dealloc method. Why is the memory not freed????????
Thanks for any suggestion, I am working on this since three whole days, it's driving me crazy.
Replace this:
self.myScrollView = nil;
self.myImage = nil;
[myScrollView release];
[myImage release];
with this:
[myScrollView release];
self.myScrollView = nil;
self.myImage = nil;
And post code where you define property myScrollView and init it.
And you don't need loop for(UIImageView *subview in [myScrollView subviews]). When you will release myScrollView object it will automatically release all its subviews.