Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory issues with NSUserDefaults
    primarykey
    data
    text
    <p>I have spent hours trying to get my project working and I just can't get it working.</p> <p>Basically, I'm trying to use NSUserDefaults to save a custom object when the user hits a save button and load all the data up when the app loads. If there is no previous NSUserDefault saved, I want to set some defaults. In the end, I am getting EXC_BAD_ACCESS when trying to load a previously-saved NSUserDefault. It works fine the first load, when setting the starting data. And the thing is, when I try to enable NSZombieEnabled and the other env vars for it, it somehow loads fine without the EXC_BAD_ACCESS. So here's what I'm working with:</p> <p><strong>[App Delegate.h]</strong></p> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import "Note.h" @interface ToDoWallAppDelegate : NSObject &lt;UIApplicationDelegate&gt; { ... Note *note; } ... @property (retain) Note *note; @end </code></pre> <p><strong>[App Delegate.m]</strong></p> <pre><code>- (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]; } </code></pre> <p><strong>[View Controller]</strong></p> <pre><code>- (void)saveNote { ... NSUserDefaults *stdDefaults = [NSUserDefaults standardUserDefaults]; if (stdDefaults) { NSData *noteData = [NSKeyedArchiver archivedDataWithRootObject:UIAppDelegate.note]; [stdDefaults setObject:noteData forKey:@"Note"]; [stdDefaults synchronize]; } } </code></pre> <p><strong>[Note.h]</strong></p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Note : NSObject &lt;NSCoding&gt; { 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 </code></pre> <p><strong>[Note.m]</strong></p> <pre><code>#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 </code></pre> <p>I really need some help I appreciate it.</p> <p>Edit:</p> <p>Btw, there are also lines from other files that edit the App Delegate's Note object, such as:</p> <pre><code>#define UIAppDelegate ((ToDoWallAppDelegate *)[UIApplication sharedApplication].delegate) ... UIAppDelegate.note.backgroundIndexPath = indexPath; </code></pre> <p>Edit:</p> <p>This is what debugger wrote:</p> <pre><code>#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 </code></pre> <p>Which are:</p> <pre><code>note.text = @"Type note here..."; //and @synthesize window, note; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload