gpt4 book ai didi

iphone - iOS 内存泄漏导致更改 View Controller 时崩溃

转载 作者:行者123 更新时间:2023-11-28 20:37:53 27 4
gpt4 key购买 nike

我创建了两个 View Controller ,其中有 UIImage 动画。它经常崩溃并在 xcode 工具中显示内存泄漏。

我的 Controller 代码-

- (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;
}

我正在通过获取 appdelegate 对象并在我的 Appdelegate.m 中调用以下 appdelegate 函数来将 viewcontrollers 更改为我的 rootviewcontroller(请提出任何好的方法)

- (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];
}

并在我各自的 Controller 按钮中调用它 -

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:@"ViewController"];

 AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:@"MainViewController"];

我想从我的主 Controller 管理 View Controller ,在没有导航栏的情况下交换 Controller 。请帮助我找出最佳方法并避免泄漏。

最佳答案

看这段代码:


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%
}

当您分配“lviewController”时,它的保留计数为 1;当你执行“self.viewController”时,我认为 viewController 是一个保留属性,然后 lviewController 保留计数增加到 2;然后你释放它,平衡之前的分配,并且保留计数返回到 1;最后你将它分配给 rootViewController,这是一个保留属性,所以 lviewController 保留计数再次为 2;最后,当您在窗口中“交换” View Controller 时,lviewController 被释放,因此它的保留计数变为 1。如您所见,它永远不会被释放。这意味着每次调用此函数时,都会发生泄漏。

关于iphone - iOS 内存泄漏导致更改 View Controller 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9750511/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com