Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-10-2012, 03:20 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 14
iph_s is on a distinguished road
Default Heap memory is growing

Today I made some tests and I am curious of the results. I made an app (ARC) which have UINavigationController and two UIViewControllers. In the first view there is a button and when that button is pressed the second view is loaded. In the second view when shake gesture is detected the first view is loaded and so on. What I notice in instruments is that the heap grows every time when a view is loaded. Here is some code

AppDelegate.m

Code:
self.navigationController = [[UINavigationController alloc]init];
self.window setRootViewController:self.navigationController];
FirstViewController *firstview = [FirstViewController alloc]init];
[self.navigationController pushViewController:FirstViewController animated:YES];
FirstViewController.m

Code:
-(IBAction)loadSecondView
{
  SecondViewController *secondview = [SecondViewController alloc]init];
  [self.navigationController pushViewController:secondview animated:YES];
}
SecondViewController.m

Code:
-(IBAction)loadFirstView
{
  FirstViewController *firstview = [FirstViewController alloc]init];
  [self.navigationController pushViewController:first view animated:YES];
}
I can't figure out why that happens. How to avoid heap of growing in that case ?
iph_s is offline   Reply With Quote
Old 02-10-2012, 03:33 AM   #2 (permalink)
New User
 
Join Date: Nov 2011
Location: Spain
Posts: 8
gmarro is on a distinguished road
Default

Quote:
Originally Posted by iph_s View Post
FirstViewController.m

Code:
-(IBAction)loadSecondView
{
  SecondViewController *secondview = [SecondViewController alloc]init];
  [self.navigationController pushViewController:secondview animated:YES];
}
SecondViewController.m

Code:
-(IBAction)loadFirstView
{
  FirstViewController *firstview = [FirstViewController alloc]init];
  [self.navigationController pushViewController:first view animated:YES];
}
I can't figure out why that happens. How to avoid heap of growing in that case ?
You are pushing second view over first view over second view over first view.... etc.

In SecondViewController you have to POP the view with popViewControllerAnimated:

Then your second view and secondViewController are both released.
gmarro is online now   Reply With Quote
Old 02-10-2012, 04:21 AM   #3 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 14
iph_s is on a distinguished road
Default

I did what you suggested. Here is the result of instruments. I made every snapshot while switching between the views.



It think the memory is still growing. Am I right ?
iph_s is offline   Reply With Quote
Old 02-10-2012, 05:00 AM   #4 (permalink)
New User
 
Join Date: Nov 2011
Location: Spain
Posts: 8
gmarro is on a distinguished road
Default

Quote:
Originally Posted by iph_s View Post
I did what you suggested. Here is the result of instruments. I made every snapshot while switching between the views.

It think the memory is still growing. Am I right ?
Can you show me the new code in SecondViewController?

Have you deleted the alloc of a new FirstViewController? You don't need it now that you are popping.
gmarro is online now   Reply With Quote
Old 02-10-2012, 05:26 AM   #5 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 14
iph_s is on a distinguished road
Default

FirstView.m

Code:
    SecondView *secondview = [[SecondView alloc]init];
    [self.navigationController pushViewController:secondview animated:YES];
SecondView.m

Code:
FirstView *firstview = [[FirstView alloc]init];
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController pushViewController:firstview animated:YES];
iph_s is offline   Reply With Quote
Old 02-10-2012, 06:54 AM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by iph_s View Post
FirstView.m

Code:
    SecondView *secondview = [[SecondView alloc]init];
    [self.navigationController pushViewController:secondview animated:YES];
SecondView.m

Code:
FirstView *firstview = [[FirstView alloc]init];
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController pushViewController:firstview animated:YES];


No. Not right. A navigation controller presents a stack of view controllers. When you push a view controller, the current view controller gets covered up by the new one, but sticks around. In order to go back to the previous view controller, just pop the new view controller off the top of the stack, without creating anything. The old view controller will still be there, and will be revealed.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 02-12-2012, 09:41 AM   #7 (permalink)
New User
 
Join Date: Nov 2011
Location: Spain
Posts: 8
gmarro is on a distinguished road
Default

Please, modify your secondView.m file:

SecondView.m

Code:
[self.navigationController popViewControllerAnimated:YES];
[/quote]

Thats all. You only have to remove the other lines.
gmarro is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 387
12 members and 375 guests
chiataytuday, chits12345, ChrisYates, fiftysixty, fredidf, gmarro, KennyChong, kilobytedump, Leslie80, Meoz, ryantcb, Yosh_K
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,670
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Yosh_K
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:28 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0