Hello,
I am trying to create a simple application to list/add/search/delete students. It is window-based application and I am using NSKeyedArchiver for data persistence.
I call NSKeyedArchiver when user tries to add a student and it works fine. It creates a file and writes into the file. But when I use NSKeyedUnarchiver to list the students the appln responds differently. It lists the students(goes to next view) sometimes and the application exits(without going to next view)other times.
Not sure why this is happening. If I comment out NSKeyedUnarchiver line it works perfectly fine but I wont be able to retrieve stored data. I even tried try,catch block but the logs indicate this.
StudentList(16260,0xa083e540) malloc: *** error for object 0x4e23000: pointer being freed was not allocated
I checked each and every release I make a release only when alloc is called.
Please find the code from Appdelegate.m below:
- (void)onButton1Clicked:(id)sender
{
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
view.backgroundColor = [UIColor lightGrayColor];
[window addSubview:view];
CGRect butRect = { 60.0, 400.0, 160.0, 40.0 };
UIButton *button = [[UIButton alloc] initWithFrame:butRect];
[button setBackgroundColor:[UIColor blueColor]];
[button setTitle:@"Back" forState: (UIControlState)UIControlStateNormal];
[button addTarget:self action:@selector(closeMe:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
[button release];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40.0, 100.0, 200.0, 30.0)];
NSMutableArray *keyArray=[[NSMutableArray alloc] init];
@try {
keyArray = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/priyahari/Documents/testfile.txt"];
}
@catch (NSString *stringException)
{
NSLog(@"provide some logs here");
}
if(keyArray != nil)
{
for(Student *s in keyArray)
{
label.text = [s fname]; //trying to print firstname alone
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
}
}
else
{
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
}
[label release];
[keyArray release];
[view release];
}
Any help would be greatly appreciated. Thanks!
Replies
//For saving
- (void) encodeWithCoder: (NSCoder *) coder
{
[coder encodeObject:fname forKey:@"fname"];
[coder encodeObject:lname forKey:@"lname"];
[coder encodeObject:ssn forKey:@"ssn"];
[coder encodeObject:pno forKey:@"pno"];
[coder encodeObject:address forKey:@"address"];
}
//For reading
-(id) initWithCoder:(NSCoder *) coder
{
fname=[[coder decodeObjectForKey: @"fname"]retain];
lname=[[coder decodeObjectForKey: @"lname"]retain];
ssn=[[coder decodeObjectForKey: @"ssn"]retain];
pno=[[coder decodeObjectForKey: @"pno"]retain];
address=[[coder decodeObjectForKey: @"address"]retain];
return self;
}
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome