AppDelegate 中的奇怪泄漏

Posted

技术标签:

【中文标题】AppDelegate 中的奇怪泄漏【英文标题】:Strange leak in the AppDelegate 【发布时间】:2013-09-22 15:00:59 【问题描述】:

为什么 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 中。

【问题讨论】:

【参考方案1】:

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

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

声称泄露了哪些对象?

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

【讨论】:

那么你的意思是负责泄漏的对象在我的控制器类中,[self.window makeKeyAndVisible]; 行就是它开始的地方? @axel 没错。 makeKeyAndVisible 可能会导致分配一堆东西作为将东西放在屏幕上的副作用。

以上是关于AppDelegate 中的奇怪泄漏的主要内容,如果未能解决你的问题,请参考以下文章

AppDelegate 类型的值没有成员 managedObjectContext

从 appdelegate 更新视图控制器 - 最佳实践?

swift : AppDelegate 和 NSManagedObjectContext 为 nil

iPhone 帮助:CoreLocation 框架中的奇怪内存泄漏

奇怪的内存泄漏

在 AppDelegate 中动画后不显示根视图控制器