简体   繁体   English

NSUserDefaults initWithSuiteName泄漏iOS 8中的内存吗?

[英]NSUserDefaults initWithSuiteName leaks memory in iOS 8?

My project uses ARC. 我的项目使用ARC。 The Leaks tool in Instruments is reporting that NSUserDefaults initWithSuiteName: leaks memory. Instruments中的泄漏工具报告NSUserDefaults initWithSuiteName:泄漏内存。

Has anyone else noticed this? 有没有其他人注意到这一点?

NSUserDefaults *theDefaults = [[NSUserDefaults alloc] initWithSuiteName:self.myGroupNameStr];

在此处输入图片说明

OK, this appears to be my own issue. 好的,这似乎是我自己的问题。

Unlike [NSUserDefaults standardUserDefaults] , my own code must assume retain ownership of [[NSUserDefaults alloc] initWithSuiteName:@"group.com.company.app"] . [NSUserDefaults standardUserDefaults]不同,我自己的代码必须假定保留[[NSUserDefaults alloc] initWithSuiteName:@"group.com.company.app"]所有权。

So the fix is to initialize self.myDefaults when my UIInputViewController loads, and then nil self.myDefaults in my UIInputViewController 's dealloc. 因此,解决方法是初始化时self.myDefaults我UIInputViewController负载,然后零self.myDefaultsUIInputViewController的dealloc的。

Create a singleton sharedManager for NSUserDefaults, and initialise the object once for the app cycle 为NSUserDefaults创建一个单例sharedManager,并在应用周期中初始化一次对象

+ (NSUserDefaults *)groupUserDefaults
{
    static NSUserDefaults *sharedGroupUserDefaults = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedGroupUserDefaults = [[self alloc] initWithSuiteName:@"group.company.appName"];        
    });
    return sharedGroupUserDefaults;
}

// try this way .// //这样尝试 。//

[[NSUserDefaults standardUserDefaults] setObject:AppDel.arrQueueForOffline forKey:@"Queued_Process"];

[[NSUserDefaults standardUserDefaults] synchronize];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM