开发者

UIViewController is correctly dealloced but its UIView is not?

开发者 https://www.devze.com 2023-01-24 11:33 出处:网络
I was using Apple\'s Instruments \"Allocations\" tool and noticed that when I went back and forth between two views, the allocation always grows.Investigating, I found that all of the memory is from o

I was using Apple's Instruments "Allocations" tool and noticed that when I went back and forth between two views, the allocation always grows. Investigating, I found that all of the memory is from objects that are subviews of my UIViewController's UIView.

I checked and my UIViewController is being dealloced, but apparently the UIView is not? I'm not retaining the UIView sepparately from the UIView controller. Anyone have any ideas?

Here's the code I wrote to switch between views.

UISubViewController *viewController = [UIViewControllerFactory createViewController:viewControllerID];     // Creates and returns a UIViewController by ID

[currentViewController viewWillDisappear:YES];
[currentViewController.view removeFromSuperview];
[currentViewController viewDidDisappear:YES];

[viewController viewWillAppear:YES];
[self.view insertSubview:viewController.view atIndex:0];
[viewController viewDidAppear:YES];

self.currentViewController = viewController;
[viewController release];

Edit:

Ok, because there was concern that I was just using a ViewController's View and then releasing the ViewController, I tried an experiment where I made a new class that is the File's Owner in the NIB, but inherits from NSObject.

the code now looks like this:

MyView *myView = [[MyView alloc] initWithNibName:@"myView"];

[currentView viewWillDisappear:YES];
[currentView.view removeFromSuperview];
[currentView viewDidDisappear:YES];

[myView viewWillAppear:YES];
[self.view insertSubview:myView.view atIndex:0];
[myView viewDidAppear:YES];

self.currentView = myView;
[myView release];

MyView's initializer looks like:

@interface MyView : NSObject {
    IBOutlet UIView *view;
    NSArray *nibTopLevelObjects;
}

@property (nonatomic, retain) UIView *view;

- (id)initWithNibN开发者_如何转开发ame:(NSString *)nibName;

@end


@implementation MyView

@synthesize view;

- (id)initWithNibName:(NSString *)nibName {
    if (self = [super init]) {

        nibTopLevelObjects = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
        [nibTopLevelObjects retain];
    }
    return self;
}

- (void)dealloc {
    [view release];
    [nibTopLevelObjects release];

    view = nil;
    nibTopLevelObjects = nil;

    [super dealloc];
}

@end

I am still seeing the memory leak of the view each time I swap in and out new views.

Any new thoughts?


Your factory returns a viewController but you are only adding a view, not pushing a new controller. Why not just create a view in this case?

What you are doing is continually overwriting the self.currentViewController ivar without releasing it. You might be releasing the view owned by that view controller but that is all.

Is currentViewController defined as

@property (nonatomic, retain) UISubViewController* currentViewController;

perhaps? If so there is a retain that isn't matched by a release - you never release self.currentViewController before overwriting it. If you didn't use self. it would not be retaining even if the property were so defined.


You're correctly removing the old view and adding a new one. This means that either somewhere in your SubViewController you're mistakenly retaining the view or you're testing in the simulator instead of the device.

Can you post your code for a SubViewController to see what's happening in there?

And do you get the same results when you test on a real device?


PS Adam Eberbach raises a fair point - why do you bother creating a view controller just to steal it's view and then release it?

0

精彩评论

暂无评论...
验证码 换一张
取 消