Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Application exits when NSKeyedUnarchiver is called!

priyaharipriyahari Posts: 3New Users
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!
Post edited by priyahari on

Replies

  • priyaharipriyahari Posts: 3New Users
    edited October 2011
    StudentArray.m

    //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;
    }
Sign In or Register to comment.