我创建了两个视图控制器,其中有UIImage
个动画。它经常崩溃,并在xcode工具中显示内存泄漏。
- (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];
}
在我各自的控制器按钮中调用它按 -
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:@"ViewController"];
OR
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate changeRootViewController:@"MainViewController"];
我想管理来自我的主控制器的视图控制器,无需使用Navigationbar即可进出控制器。请帮助我找出最佳方法并避免泄漏。
答案 0 :(得分:2)
看看这段代码:
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; 然后你释放它,平衡先前的alloc,并保持count返回1; 最后你将它分配给rootViewController,这是一个保留属性,所以lviewController再次保留计数为2; 最后当你在窗口中“交换”视图控制器时,lviewController被释放,因此它保持计数变为1.正如你所看到的,它永远不会被释放。这意味着每次调用此函数时,都会泄漏。
答案 1 :(得分:1)
您需要在代码中更改一些语句..
这条线没有任何意义..
imgView = UIViewContentModeScaleToFill;
使用一个函数,你必须在UIImageView
文档中将UIViewContentModeScaleToFill
属性的值设置为UIImageView
。
[imageView setContentMode:UIViewContentModeScaleToFill];
与此相同......
lviewController.view = nil; //Remove this from your code ..
最后更改dealloc函数的implmentation并记住[super dealloc];
应该是dealloc
的任何实现中的最后一个。
- (void)dealloc {
[imgView release];
imgView = nil;
[super dealloc];
}
答案 2 :(得分:0)
可能的泄漏是由于“SecondViewController”,“MainViewController”,“ViewController”类中的代码造成的。崩溃的原因是您在[super dealloc]
之后发布了图像视图试试这个,
- (void)dealloc {
[imgView release];
imgView = nil;
[super dealloc];
}