NSUserDefaults的内存问题

时间:2010-07-07 23:22:24

标签: iphone xcode memory nsuserdefaults exc-bad-access

我花了好几个小时试图让我的项目工作,但我无法让它工作。

基本上,当用户点击保存按钮并在应用加载时加载所有数据时,我正在尝试使用NSUserDefaults来保存自定义对象。如果没有保存以前的NSUserDefault,我想设置一些默认值。最后,我在尝试加载以前保存的NSUserDefault时收到EXC_BAD_ACCESS。设置起始数据时,它在第一次加载时工作正常。问题是,当我尝试为它启用NSZombieEnabled和其他env变量时,它会在没有EXC_BAD_ACCESS的情况下以某种方式加载。所以这就是我正在做的事情:

[App Delegate.h]

#import <UIKit/UIKit.h>
#import "Note.h"

@interface ToDoWallAppDelegate : NSObject <UIApplicationDelegate> {
    ...
    Note *note;
}

...
@property (retain) Note *note;

@end

[App Delegate.m]

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    ...
    note = [[Note alloc] init];

    NSUserDefaults *stdDefaults = [NSUserDefaults standardUserDefaults];
    NSData *noteData = [stdDefaults objectForKey:@"Note"];
    if (noteData) {
        self.note = (Note *)[NSKeyedUnarchiver unarchiveObjectWithData:noteData];
    } else {
        note.background = [UIImage imageNamed:@"Cork.jpg"];
        note.picture = [UIImage imageNamed:@"Cork.jpg"];
        note.font = [UIFont fontWithName:@"Helvetica" size:18.0f];
        note.fontColor = [UIColor blackColor];
        note.fontNameIndex = 9;
        note.fontSizeIndex = 6;
        note.fontColorIndex = 0;
        note.backgroundIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        note.pictureIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        note.text = @"Type note here...";
    }

    ...
}

- (void)dealloc {
    ...
    [note release];
    [super dealloc];
}

[查看控制器]

- (void)saveNote {
    ...
    NSUserDefaults *stdDefaults = [NSUserDefaults standardUserDefaults];
    if (stdDefaults) {
        NSData *noteData = [NSKeyedArchiver archivedDataWithRootObject:UIAppDelegate.note];
        [stdDefaults setObject:noteData forKey:@"Note"];
        [stdDefaults synchronize];
    }
}

[Note.h]

#import <Foundation/Foundation.h>


@interface Note : NSObject <NSCoding> {
    UIImage *background, *picture;
    UIFont *font;
    UIColor *fontColor;
    int fontNameIndex, fontSizeIndex, fontColorIndex;
    NSIndexPath *backgroundIndexPath, *pictureIndexPath;
    BOOL customBackground;
    NSString *text;
}

@property (retain) UIImage *background, *picture;
@property (retain) UIFont *font;
@property (retain) UIColor *fontColor;
@property int fontNameIndex, fontSizeIndex, fontColorIndex;
@property (retain) NSIndexPath *backgroundIndexPath, *pictureIndexPath;
@property BOOL customBackground;
@property (retain) NSString *text;

- (Note *)init;

@end

[Note.m]

#import "Note.h"


@implementation Note

@synthesize background, picture, font, fontColor, fontNameIndex, fontSizeIndex, fontColorIndex, customBackground, backgroundIndexPath, pictureIndexPath, text;

- (Note *)init {
    if (self = [super init]) {
        background = [[UIImage alloc] init];
        picture = [[UIImage alloc] init];
        font = [[UIFont alloc] init];
        fontColor = [[UIColor alloc] init];
        backgroundIndexPath = [[NSIndexPath alloc] init];
        pictureIndexPath = [[NSIndexPath alloc] init];
        text = [[NSString alloc] init];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    NSData *dataBackground = UIImagePNGRepresentation(background);
    NSData *dataPicture = UIImagePNGRepresentation(picture);

    [encoder encodeObject:dataBackground forKey:@"dataBackground"];
    [encoder encodeObject:dataPicture forKey:@"dataPicture"];
    [encoder encodeObject:font forKey:@"font"];
    [encoder encodeObject:fontColor forKey:@"fontColor"];
    [encoder encodeInt:fontSizeIndex forKey:@"fontSizeIndex"];
    [encoder encodeInt:fontColorIndex forKey:@"fontColorIndex"];
    [encoder encodeBool:customBackground forKey:@"customBackground"];
    [encoder encodeObject:backgroundIndexPath forKey:@"backgroundIndexPath"];
    [encoder encodeObject:pictureIndexPath forKey:@"pictureIndexPath"];
    [encoder encodeObject:text forKey:@"text"];
}

- (Note *)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        NSData *dataBackground = [decoder decodeObjectForKey:@"dataBackground"];
        NSData *dataPicture = [decoder decodeObjectForKey:@"dataPicture"];
        background = [[UIImage imageWithData:dataBackground] retain];
        picture = [[UIImage imageWithData:dataPicture] retain];
        font = [[decoder decodeObjectForKey:@"font"] retain];
        fontColor = [[decoder decodeObjectForKey:@"fontColor"] retain];
        fontNameIndex = [decoder decodeIntForKey:@"fontNameIndex"];
        fontColorIndex = [decoder decodeIntForKey:@"fontColorIndex"];
        customBackground = [decoder decodeBoolForKey:@"customBackground"];


    backgroundIndexPath = [[decoder decodeObjectForKey:@"backgroundIndexPath"] retain];
        text = [[decoder decodeObjectForKey:@"text"] retain];
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
    [background release];
    [picture release];
    [font release];
    [fontColor release];
    [backgroundIndexPath release];
    [pictureIndexPath release];
    [text release];
}

@end

我真的需要一些帮助,我很感激。

编辑:

顺便说一句,还有来自其他文件的行编辑App Delegate的Note对象,例如:

#define UIAppDelegate ((ToDoWallAppDelegate *)[UIApplication sharedApplication].delegate)
...
UIAppDelegate.note.backgroundIndexPath = indexPath;

编辑:

这是调试器写的:

#0  0x90be9ed7 in objc_msgSend
#1  0x03b05210 in ??
#2  0x000023ce in -[ToDoWallAppDelegate setNote:] at ToDoWallAppDelegate.m:14
#3  0x00002216 in -[ToDoWallAppDelegate applicationDidFinishLaunching:] at ToDoWallAppDelegate.m:35

这是:

note.text = @"Type note here...";
//and
@synthesize window, note;

2 个答案:

答案 0 :(得分:7)

我不确定这是否会导致您的问题,但我相信[super dealloc]应该是您的dealloc方法的最后一行,而不是第一行。

答案 1 :(得分:1)

查看文档如何设置默认值 Using NSUserDefaults

如果您需要更多信息,请查看Hillega的书“Mac OS X的Cocoa编程”bignerdranch.com/books,在那里进行了解释。

您应该考虑将标题更改为“如何使用NSUserDefaults”...

如何设置默认值的示例,在initialize中的类中添加如下内容:

+ (void)initialize{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *appDefaults = [NSDictionary
        dictionaryWithObject:@"YES" forKey:@"DeleteBackup"];

    [defaults registerDefaults:appDefaults];
}

您可以发布确切错误的位置,而不是发布整个代码。通过调试器运行它,看看它停在哪里。