1

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

仪器截图

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 中。

4

1 回答 1

3

泄漏工具仅告诉您导致分配泄漏的代码行,而不是泄漏的原因。在这种情况下,由于它是一种运行一次的方法,并且假定它是一个旨在在您的应用程序的生命周期内一直存在的单个对象,因此这不是问题。

但是,您应该修复它,但这里没有足够的信息来建议修复。

声称泄露了哪些对象?

此外,做类似@synthesize window = hWindow. 不要声明 ivar,让编译器自动合成_window。这将导致代码更接近标准模式。

于 2013-09-22T15:29:23.283 回答