AppDelegate中的奇怪泄漏

时间:2013-09-22 15:00:59

标签: ios objective-c memory-leaks instruments

为什么Instruments会在AppDelegate类的以下代码行中检测到泄漏?以下是“泄漏”工具的屏幕截图,下面是源代码。

Instruments Screenshot

AppDelegate实施文件:

#import "AppDelegate.h"
#import "Strings.h"

@implementation AppDelegate
@synthesize window = hWindow;
- (void)dealloc
{
    [hWindow release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
   // [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone)
    {    // The iOS device = iPhone or iPod Touch

        CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
        UIViewController *initialViewController = nil;
        if (iOSDeviceScreenSize.height == 480)
        {   // iPhone 3GS, 4, and 4S and iPod Touch 3rd and 4th generation: 3.5 inch screen (diagonally measured)

            // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone35
            UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"Storyboard_Retina3.5" bundle:nil];

            // Instantiate the initial view controller object from the storyboard
            initialViewController = [iPhone35Storyboard instantiateInitialViewController];
        }

        if (iOSDeviceScreenSize.height == 568)
        {   // iPhone 5 and iPod Touch 5th generation: 4 inch screen (diagonally measured)

            // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
            UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];

            // Instantiate the initial view controller object from the storyboard
            initialViewController = [iPhone4Storyboard instantiateInitialViewController];
        }
        // Instantiate a UIWindow object and initialize it with the screen size of the iOS device
        [self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];

        // Set the initial view controller to be the root view controller of the window object
        self.window.rootViewController  = initialViewController;

        // Set the window object to be the key window and show it

    } else if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {   // The iOS device = iPad
        UIViewController *initialViewController = nil;
        UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];

        // Instantiate the initial view controller object from the storyboard
        initialViewController = [iPhone4Storyboard instantiateInitialViewController];
        [self setWindow:[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]];

        // Set the initial view controller to be the root view controller of the window object
        self.window.rootViewController  = initialViewController;
        // Set the window object to be the key window and show it

        [[UIApplication sharedApplication] setStatusBarHidden:NO];
    }
    [[self window] makeKeyAndVisible];
    NSLog(@"%d",[[self window] retainCount]);
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 70000
    //iOS 7 only stuff here
#endif
    return YES;
}
.....
@end

AppDelegate头文件:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UIWindow *hWindow;
}
@property (strong, nonatomic) UIWindow *window;

@end

可能原因是该应用程序仍在运行且未终止才能解除分配?我想提一下,在Xcode 4中我没有得到这样的泄漏,它开始出现在Xcode 5中。

1 个答案:

答案 0 :(得分:3)

泄漏工具只告诉您导致泄漏的代码行,泄漏的原因。在这种情况下,因为它是一次运行方法,并且假设它是一个设计用于应用程序生命周期的单个对象,所以它不是问题。

然而,这是你应该修复的东西,但是这里没有足够的信息来建议修复。

声称泄露了哪些对象?

此外,执行@synthesize window = hWindow之类的事情也是非典型的。不要声明ivar,只需让编译器自动合成_window。这将使代码更接近标准模式。