gpt4 book ai didi

iphone - 推送 View Controller : Is this a memory leak?

转载 作者:行者123 更新时间:2023-11-29 04:51:47 25 4
gpt4 key购买 nike

我正在使用最新的 SDK 和 XCode 4.2 开发 iOS 4(我使用ARC)。

我正在以编程方式开发导航 Controller ,我有一个问题。

这是AppDelegate.h

#import <UIKit/UIKit.h>

@class ViewController;
@class SecondViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController* navController;
ViewController* viewController;
SecondViewController* secondViewController;
}

@property (strong, nonatomic) UIWindow *window;

- (void) showSecondViewController;

@end

这是 AppDelegate.m

    #import "AppDelegate.h"

#import "ViewController.h"
#import "SecondViewController.h"

@implementation AppDelegate

@synthesize window = _window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
viewController.title = @"Menu";
navController = [[UINavigationController alloc] initWithRootViewController:viewController];
navController.navigationBar.tintColor = [UIColor blackColor];
self.window.rootViewController = navController;

[self.window makeKeyAndVisible];
return YES;
}

- (void) dealloc
{
[_window release];
[viewController release];
[navController release];
[secondViewController release];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
...
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
...
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
...
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
...
}

- (void)applicationWillTerminate:(UIApplication *)application
{
...
}

- (void) showSecondViewController
{
secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.title = @"Second";
[navController pushViewController:secondViewController animated:YES];
}

我的问题是关于最后一个方法,-(void)showSecondViewController;

我可以在最后添加这一行吗?

[secondViewController release]

我已经分析了该应用程序,并且没有发现任何内存泄漏。但我必须在这里问,因为我不确定。

最佳答案

如果再次调用showSecondViewController方法,将会出现内存泄漏。

您应该在 showSecondViewController 方法中释放 secondViewController

- (void) showSecondViewController
{
secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.title = @"Second";
[navController pushViewController:secondViewController animated:YES];
[secondViewController release]
}

当您执行 pushViewController:secondViewController

时,它会自动被 navController 保留

关于iphone - 推送 View Controller : Is this a memory leak?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677490/

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