简体   繁体   中英

iPhone memory management

I am having problems in my app with memory management. There are some concepts that I do not understand. For example, what is wrong with this code? How should I deal with this issue?

CustomController.h:

@interface CustomController : UIViewController <UIScrollViewDelegate>
{       
  IBOutlet UIScrollView  *scroll_view;
}

@property (nonatomic, retain) UIScrollView *scroll_view;

@end

CustomController.m:

@implementation CustomController

@synthesize scroll_view;

- (void)viewDidLoad 
{   
    [self setup_content];
    [super viewDidLoad];
}

- (void) setup_content
{
  // Fill the scrollview with some subviews
}

- (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 
{
  [scroll_view release];
  scroll_view = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc 
{    
    [scroll_view  release];
    [super          dealloc];
}

@end

Using it:

CustomController *custom_controller = [[CustomController alloc] initWithNibName: @"CustomController" bundle: nil];
NSLog(@"Controller: %i", [custom_controller retainCount]);
NSLog(@"ScrollView: %i", [custom_controller.scroll_view retainCount]);

[controllersArray addObject: custom_controller];
NSLog(@"Controller: %i", [custom_controller retainCount]);
NSLog(@"ScrollView: %i", [custom_controller.scroll_view retainCount]);

[custom_controller release];
NSLog(@"Controller: %i", [custom_controller retainCount]);
NSLog(@"ScrollView: %i", [custom_controller.scroll_view retainCount]);

And the output:

Controller: 1
ScrollView: 0

Controller: 2
ScrollView: 2

Controller: 1
ScrollView: 2

At this point, when I remove the controller from the array, becouse I do not need it anymore, the retainCount that scrollView has in Controller's dealloc method is 2 and the memory is not freed (scrollView has lots of uiimageviews that are not being released). Somebody could help please?

Edit:

Finally the problem was that the controllers view was being retained by a navigation controller. I was using the controllers array as a stack for a custom navigation. In the pop, I was removing the controller from the array however the view was still retained by the navigation controller. Removing it from superview fix the problem and now my scrollView and all the subviews it has, are released correctly! Thank you everybody!

You private variable for the IBOutlet is called scroll_view while the property is called m_scroll_view.

Then you will need to tell the @@synthesize the the property should use the correct private variable:

@synthesize m_scroll_view =  scroll_view;

(The edit removed this)

You should also first call any super methods before calling your own, except when calling dealloc.

@interface CustomController : UIViewController <UIScrollViewDelegate>
{       
  IBOutlet UIScrollView  *scroll_view;
}

@property (nonatomic, retain) UIScrollView *scroll_view;

its good to have another name for the ivar than the property eg _scroll_view, that way you can see which is which. Normally I use self.property when addressing properties, makes it easier to see that its a property and use _ivar when its an ivar.

@interface CustomController : UIViewController <UIScrollViewDelegate>
{       
  IBOutlet UIScrollView  *_scroll_view;
}

@property (nonatomic, retain) UIScrollView *scroll_view = _scroll_view;

Your test code does not show what happens after you also remove it from the array. In that case the retain count on the controller should go to 0 and the scrollview should go to 1. I suspect the reason scrollview will still have a retain count is that it is still being referenced in the view hierarchy. If you remove the view it should then be deallocated.

You can also add a breakpoint in the retain message for the scrollview to see who is retaining it and make sure they also release it.

the controllersArray is still holding your custom_controller thats y the retain count is 1. when you remove that object from the array it should drop down to 0 and then the scroller will be released.

so you should try [controllersArray removeObject:custom_controller] when your done with it. assuming that controllersArray is NSMutableArray of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM