不知怎的,我的单例属性被释放了

发布于 2024-09-15 20:32:44 字数 6476 浏览 1 评论 0原文

我有一个名为 PoolManager 的单例对象,它在 plist 中加载和保存一些数据。在我的程序中,当需要了解我的池时,它会向 [PoolManager sharedPoolManager] 询问其属性。我有一个负责设置这些属性的视图,而所有其他视图只是从中读取。一切都工作正常,然后不知为何,它开始崩溃。我设置了 NSZombieEnabled = YES 并可以看到,当我访问两个 NSString 属性之一时,它们似乎已被释放。调试器消息是: *** -[CFString respondsToSelector:]: message sent to deallocated instance 0x5a336d0

我尝试返回到之前的快照,一切正常,但仍然如此。我什至使用 TimeMachine 回到昨天的项目,它也做到了。我很困惑。

这是单例对象代码...它的 surfaceshape 字符串显然是僵尸。对于所有 NSLogs 表示抱歉


//  MyPoolSingleton.h

#import <Foundation/Foundation.h>

#define kFileName @"data.plist"

@interface PoolManager : NSObject {
    float   volume;
    float length;
    float width;
    float depth;    
    NSString *surface;
    NSString *shape;
    BOOL isMetric;
    int fcTarget;
    int cyaTarget;
    int taTarget;
    int chTarget;
    int saltTarget;
}

@property float volume;
@property float length;
@property float width;
@property float depth;
@property (nonatomic, retain) NSString *surface;
@property (nonatomic, retain) NSString *shape;
@property BOOL isMetric;
@property int fcTarget;
@property int cyaTarget;
@property int taTarget;
@property int chTarget;
@property int saltTarget;

+ (PoolManager*)sharedPoolManager;
- (void)retrieveState;
- (void)saveState;
- (NSString*)dataFilePath;

@end

//  MyPoolSingleton.m

#import "PoolManager.h"

@implementation PoolManager

@synthesize volume;
@synthesize length;
@synthesize width;
@synthesize depth;
@synthesize surface;
@synthesize shape;
@synthesize isMetric;
@synthesize fcTarget;
@synthesize cyaTarget;
@synthesize taTarget;
@synthesize chTarget;
@synthesize saltTarget;

static PoolManager* _sharedPoolManager = nil;

+ (PoolManager*)sharedPoolManager {
    @synchronized([PoolManager class])
    {
        if (!_sharedPoolManager)
            [[self alloc] init];
        return _sharedPoolManager;
    }
    return nil;
}

+ (id)alloc {
    @synchronized([PoolManager class])
    {
        NSAssert(_sharedPoolManager == nil, @"Attempted to allocate a second instance of a singleton.");
        _sharedPoolManager = [super alloc];
        return _sharedPoolManager;
    }
    return nil;
}

- (id)init {
    self = [super init];
    return self;
}

- (void)retrieveState {
    NSLog(@"--retrieveState");
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSLog(@"    fileExistsAtPath: reading array from plist");
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        volume = [[array objectAtIndex:0] floatValue];
            NSLog(@"      reading array: volume = %1.1f", volume);
        length = [[array objectAtIndex:1] floatValue];
            NSLog(@"      reading array: length = %1.1f", length);
        width = [[array objectAtIndex:2] floatValue];
            NSLog(@"      reading array: width = %1.1f", width);
        depth = [[array objectAtIndex:3] floatValue];
            NSLog(@"      reading array: depth = %1.1f", depth);
        self.surface = [array objectAtIndex:4];
            NSLog(@"      reading array: surface = %@", surface);
        self.shape = [array objectAtIndex:5];
            NSLog(@"      reading array: shape = %@", shape);
        isMetric = [[array objectAtIndex:6] boolValue];
            NSLog(@"      reading array: isMetric = %d", isMetric);
        fcTarget = [[array objectAtIndex:7] intValue];
            NSLog(@"      reading array: fcTarget = %d", fcTarget);
        cyaTarget = [[array objectAtIndex:8] intValue];
            NSLog(@"      reading array: cyaTarget = %d", cyaTarget);
        taTarget = [[array objectAtIndex:9] intValue];
            NSLog(@"      reading array: taTarget = %d", taTarget);
        chTarget = [[array objectAtIndex:10] intValue];
            NSLog(@"      reading array: chTarget = %d", chTarget);
        saltTarget = [[array objectAtIndex:11] intValue];
            NSLog(@"      reading array: saltTarget = %d", saltTarget);
        [array release];
    }
    else {
        NSLog(@"    !fileExistsAtPath: intitializing values to nil/zero");
        volume = 0.0;
        length = 0.0;
        width = 0.0;
        depth = 0.0;
        surface = @"";
        shape = @"";
        isMetric = NO;
        fcTarget = 0.0;
        cyaTarget = 0.0;
        taTarget = 0.0;
        chTarget = 0.0;
        saltTarget = 0.0;
    }
}

- (void)saveState {
    NSLog(@"--saveState");
    NSMutableArray *array = [[NSMutableArray alloc] init];
        NSLog(@"      building array: volume = %1.1f", volume);
    [array addObject:[NSNumber numberWithFloat:volume]];
        NSLog(@"      building array: length = %1.1f", length);
    [array addObject:[NSNumber numberWithFloat:length]];
        NSLog(@"      building array: width = %1.1f", width);
    [array addObject:[NSNumber numberWithFloat:width]];
        NSLog(@"      building array: depth = %1.1f", depth);
    [array addObject:[NSNumber numberWithFloat:depth]];
        NSLog(@"      building array: surface = %@", surface);
    [array addObject:surface];
        NSLog(@"      building array: shape = %@", shape);
    [array addObject:shape];
        NSLog(@"      building array: isMetric = %d", isMetric);
    [array addObject:[NSNumber numberWithBool:isMetric]];
        NSLog(@"      building array: fcTarget = %d", fcTarget);
    [array addObject:[NSNumber numberWithInt:fcTarget]];
        NSLog(@"      building array: cyaTarget = %d", cyaTarget);
    [array addObject:[NSNumber numberWithInt:cyaTarget]];
        NSLog(@"      building array: taTarget = %d", taTarget);
    [array addObject:[NSNumber numberWithInt:taTarget]];
            NSLog(@"      building array: chTarget = %d", chTarget);
    [array addObject:[NSNumber numberWithInt:chTarget]];
        NSLog(@"      building array: saltTarget = %d", saltTarget);
    [array addObject:[NSNumber numberWithInt:saltTarget]];

    [array writeToFile:[self dataFilePath] atomically:YES];
    [array release];
}    

- (NSString*)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFileName];
}

- (void)dealloc {
    [shape release], shape = nil;
    [surface release], surface = nil;
    [super dealloc];
}

@end

I have a singleton object called PoolManager that loads and saves some data in a plist. Throughout my program when something needs to know about my pool, it asks the [PoolManager sharedPoolManager] for it's properties. I have a single view that's responsible for setting these properties and all others just read from it. It was all working fine, and then for no reason I can tell, it started crashing. I set NSZombieEnabled = YES and can see that when I access one of the two NSString properties, they appear to have been released. The debugger message is: *** -[CFString respondsToSelector:]: message sent to deallocated instance 0x5a336d0

I tried going back to a previous snapshot where everything worked and it still does this. I even used TimeMachine to go back to the project from yesterday and it does it too. I'm baffled.

Here is the singleton object code... It's the surface and shape strings that are apparently zombies. Sorry for all the NSLogs


//  MyPoolSingleton.h

#import <Foundation/Foundation.h>

#define kFileName @"data.plist"

@interface PoolManager : NSObject {
    float   volume;
    float length;
    float width;
    float depth;    
    NSString *surface;
    NSString *shape;
    BOOL isMetric;
    int fcTarget;
    int cyaTarget;
    int taTarget;
    int chTarget;
    int saltTarget;
}

@property float volume;
@property float length;
@property float width;
@property float depth;
@property (nonatomic, retain) NSString *surface;
@property (nonatomic, retain) NSString *shape;
@property BOOL isMetric;
@property int fcTarget;
@property int cyaTarget;
@property int taTarget;
@property int chTarget;
@property int saltTarget;

+ (PoolManager*)sharedPoolManager;
- (void)retrieveState;
- (void)saveState;
- (NSString*)dataFilePath;

@end

//  MyPoolSingleton.m

#import "PoolManager.h"

@implementation PoolManager

@synthesize volume;
@synthesize length;
@synthesize width;
@synthesize depth;
@synthesize surface;
@synthesize shape;
@synthesize isMetric;
@synthesize fcTarget;
@synthesize cyaTarget;
@synthesize taTarget;
@synthesize chTarget;
@synthesize saltTarget;

static PoolManager* _sharedPoolManager = nil;

+ (PoolManager*)sharedPoolManager {
    @synchronized([PoolManager class])
    {
        if (!_sharedPoolManager)
            [[self alloc] init];
        return _sharedPoolManager;
    }
    return nil;
}

+ (id)alloc {
    @synchronized([PoolManager class])
    {
        NSAssert(_sharedPoolManager == nil, @"Attempted to allocate a second instance of a singleton.");
        _sharedPoolManager = [super alloc];
        return _sharedPoolManager;
    }
    return nil;
}

- (id)init {
    self = [super init];
    return self;
}

- (void)retrieveState {
    NSLog(@"--retrieveState");
    NSString *filePath = [self dataFilePath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        NSLog(@"    fileExistsAtPath: reading array from plist");
        NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
        volume = [[array objectAtIndex:0] floatValue];
            NSLog(@"      reading array: volume = %1.1f", volume);
        length = [[array objectAtIndex:1] floatValue];
            NSLog(@"      reading array: length = %1.1f", length);
        width = [[array objectAtIndex:2] floatValue];
            NSLog(@"      reading array: width = %1.1f", width);
        depth = [[array objectAtIndex:3] floatValue];
            NSLog(@"      reading array: depth = %1.1f", depth);
        self.surface = [array objectAtIndex:4];
            NSLog(@"      reading array: surface = %@", surface);
        self.shape = [array objectAtIndex:5];
            NSLog(@"      reading array: shape = %@", shape);
        isMetric = [[array objectAtIndex:6] boolValue];
            NSLog(@"      reading array: isMetric = %d", isMetric);
        fcTarget = [[array objectAtIndex:7] intValue];
            NSLog(@"      reading array: fcTarget = %d", fcTarget);
        cyaTarget = [[array objectAtIndex:8] intValue];
            NSLog(@"      reading array: cyaTarget = %d", cyaTarget);
        taTarget = [[array objectAtIndex:9] intValue];
            NSLog(@"      reading array: taTarget = %d", taTarget);
        chTarget = [[array objectAtIndex:10] intValue];
            NSLog(@"      reading array: chTarget = %d", chTarget);
        saltTarget = [[array objectAtIndex:11] intValue];
            NSLog(@"      reading array: saltTarget = %d", saltTarget);
        [array release];
    }
    else {
        NSLog(@"    !fileExistsAtPath: intitializing values to nil/zero");
        volume = 0.0;
        length = 0.0;
        width = 0.0;
        depth = 0.0;
        surface = @"";
        shape = @"";
        isMetric = NO;
        fcTarget = 0.0;
        cyaTarget = 0.0;
        taTarget = 0.0;
        chTarget = 0.0;
        saltTarget = 0.0;
    }
}

- (void)saveState {
    NSLog(@"--saveState");
    NSMutableArray *array = [[NSMutableArray alloc] init];
        NSLog(@"      building array: volume = %1.1f", volume);
    [array addObject:[NSNumber numberWithFloat:volume]];
        NSLog(@"      building array: length = %1.1f", length);
    [array addObject:[NSNumber numberWithFloat:length]];
        NSLog(@"      building array: width = %1.1f", width);
    [array addObject:[NSNumber numberWithFloat:width]];
        NSLog(@"      building array: depth = %1.1f", depth);
    [array addObject:[NSNumber numberWithFloat:depth]];
        NSLog(@"      building array: surface = %@", surface);
    [array addObject:surface];
        NSLog(@"      building array: shape = %@", shape);
    [array addObject:shape];
        NSLog(@"      building array: isMetric = %d", isMetric);
    [array addObject:[NSNumber numberWithBool:isMetric]];
        NSLog(@"      building array: fcTarget = %d", fcTarget);
    [array addObject:[NSNumber numberWithInt:fcTarget]];
        NSLog(@"      building array: cyaTarget = %d", cyaTarget);
    [array addObject:[NSNumber numberWithInt:cyaTarget]];
        NSLog(@"      building array: taTarget = %d", taTarget);
    [array addObject:[NSNumber numberWithInt:taTarget]];
            NSLog(@"      building array: chTarget = %d", chTarget);
    [array addObject:[NSNumber numberWithInt:chTarget]];
        NSLog(@"      building array: saltTarget = %d", saltTarget);
    [array addObject:[NSNumber numberWithInt:saltTarget]];

    [array writeToFile:[self dataFilePath] atomically:YES];
    [array release];
}    

- (NSString*)dataFilePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFileName];
}

- (void)dealloc {
    [shape release], shape = nil;
    [surface release], surface = nil;
    [super dealloc];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾旎 2024-09-22 20:32:44

objectAtIndex: 给出一个自动释放的对象。您应该保留它,或者在设置它们时使用属性访问器 self.surface = ...self.shape = ...

objectAtIndex: gives an autoreleased object. You should either retain it, or use the property accessor self.surface = ... and self.shape = ... when setting those.

一腔孤↑勇 2024-09-22 20:32:44

就我个人而言,我更喜欢以下单例模式:(

+ (id) sharedPoolManager
{
    static id sharedPoolManager = nil;
    @synchronized (self) {
        if (sharedPoolManager == nil) {
             sharedPoolManager = [self new];
        }
    }
    return sharedPoolManager;
}

- (id) init
{
    if ((self = [super init]) != nil) {
         // Initialize ... nothing special here ...
    }
    return self;
}

请注意,类方法中的 self 相当于 [SomeClass class]

上面更简洁,我更喜欢保留任何initalloc 之外的单例代码,因为如果需要,我还可以创建多个实例。例如在单元测试中。

就我个人而言,我认为您不必保护程序员不创建多个实例。契约很明确:sharedPoolManager 返回一个单例实例。如果这就是您想要/需要的,那么就使用它。否则以通常的方式创建实例。

Personally I prefer the following pattern for singletons:

+ (id) sharedPoolManager
{
    static id sharedPoolManager = nil;
    @synchronized (self) {
        if (sharedPoolManager == nil) {
             sharedPoolManager = [self new];
        }
    }
    return sharedPoolManager;
}

- (id) init
{
    if ((self = [super init]) != nil) {
         // Initialize ... nothing special here ...
    }
    return self;
}

(Note that self in a class method is equivalent to [SomeClass class])

The above is more concise and I prefer to keep any singleton code outside of init and alloc since I can then also create multiple instances if needed. For example in unit tests.

Personally I don't think you have to protect the programmer from creating multiple instances. The contract is clear: sharedPoolManager returns a singleton instance. If that is what you want/need then use that. Otherwise create instances the usual way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文