大家好,我开发了一款iPad应用,问题是这样的,我第一次打开应用后,并没有关闭我的应用,按两次HOME键,我的应用图标在黎明,当我按了HOME键或其他操作,回到主界面看其他应用内容时,再此点击我的应用图标时或按两次HOME键,点击我的应用图标,之前还处理打开状态的我的应用就会闪退,完全重启打开我的应用,一般情况下,当应用没有关闭,第二次打开时,会直接打开上次应用的界面,我不知道问题出在什么地方,请高手指点。main.m代码如下:#import <UIKit/UIKit.h>
#import "AppDelegate.h"int main(int argc, char *argv[])
{
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
     //int retVal = UIApplicationMain(argc, argv, nil, nil);
     [pool release];
     return retVal;
     
}AppDelegate.m代码如下:#import "AppDelegate.h"#import "ViewController.h"
#import "LeadingViewControllerViewController.h"@implementation AppDelegate@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize leadingViewController = _leadingViewController;- (void)dealloc
{
    [_window release];
    [_viewController release];
    [_leadingViewController release];
    [super dealloc];
}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //在启动画面消失前增加3秒延时  
    [NSThread sleepForTimeInterval:3]; 
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];
    } 
    else
    {
        self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil] autorelease];
    }
    
    [self.window addSubview:self.viewController.view];
    
    
    if (![self getUsed]) 
    {
        self.leadingViewController = [[[LeadingViewControllerViewController alloc] init] autorelease];
        [self.window addSubview:self.leadingViewController.view];
        [self saveUsed];
    }
     
    /*
     self.leadingViewController = [[[LeadingViewControllerViewController alloc] init] autorelease];
     [self.window addSubview:self.leadingViewController.view];
     [self saveUsed];*/
    
    [self.window makeKeyAndVisible];
    return YES;
}//存储用户第一次使用该app的信息
- (void) saveUsed 
{
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    if (standardUserDefaults) 
    {
        [standardUserDefaults setBool:TRUE forKey:@"RhineSplash1.9_Used"];
        [standardUserDefaults synchronize];
    }
}//获取用户是否已经使用过该app
- (BOOL) getUsed 
{
    BOOL val = NO;
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    if (standardUserDefaults) 
    {    
        val = [standardUserDefaults boolForKey:@"RhineSplash1.9_Used"];
    }
    return val;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}@end

解决方案 »

  1.   

    @synthesize viewController = _viewController;[_viewController release];
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];viewController释放两次了!
      

  2.   

    这里应该是释放一次,确认这些参数
    _window、_viewController、_leadingViewController设置的属性声明有没有设置成assign
      
      

  3.   

    建议楼主用ARC,基本上不需要担心内存泄露了。可以通过Xcode中的 Edit/Refactor/Convert to Ojbective-C ARC 来实现把代码升级到ARC
      

  4.   

    initWithNibName:@"ViewController_iPhone" bundle:nil
    这个不太用,你看看是不是XIB文件对应上了。//在启动画面消失前增加3秒延时  
        [NSThread sleepForTimeInterval:3];这个是不是也是一个原因貌似真机 启动,3秒的停留 会让 系统认为 可以关闭这个app 了其他的具体也看不出什么问题,你设置断点看看,log打印出什么东西,加以分析。