ios - AppDelegate中发生奇怪的泄漏

标签 ios objective-c memory-leaks 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中。

最佳答案

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

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

声称泄漏了哪些物体?

同样,它是非典型的事情,比如@synthesize window = hWindow。不要声明ivar,而只是让编译器自动合成_window。这将导致代码更接近标准模式。

关于ios - AppDelegate中发生奇怪的泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18945195/

相关文章:

ios - 图像在 iOS 上显示为按钮

ios - Titanium - 像Groupon一样的滑动菜单

iphone - 'Appdelegate' 没有可见的@interface 声明选择器 'setupTabBarController'

ios - Objective-C - 类实例在哪里声明?

java - 分析内存不足错误的堆转储

iphone - 为什么 Instruments "Leak"模板无法捕获我的 iOS 应用程序上的内存泄漏?

ios - 如何创建嵌入框架以便我可以在应用程序扩展和项目中使用它

ios - FacebookShare 0.3.2 Swift ShareDialog 控制台错误

python - 在python中反序列化objective-c二进制NSMutableArray

ios - 需要帮助修复内存泄漏 - NSMutableString