iphone - App Crashing- Core Data Problem Or Memory Management? -
console message:
had use picture because wasn't formatting correctly in post text.
the received memory warning level 2 showed before app crashed.
errror comes @ line - cell.textlabel.text = temproutine.name;
link full-size picture (http://www.box.net/shared/static/7igj3r4trh.png)

viewcontroller:
@implementation routinetableviewcontroller @synthesize tableview; @synthesize eventsarray; @synthesize entered; @synthesize managedobjectcontext; #pragma mark - view lifecycle - (void)viewdidload { if (managedobjectcontext == nil) { managedobjectcontext = [(curlappdelegate *)[[uiapplication sharedapplication] delegate] managedobjectcontext]; } nsfetchrequest *request = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"routine" inmanagedobjectcontext:managedobjectcontext]; [request setentity:entity]; nserror *error = nil; nsmutablearray *mutablefetchresults = [[managedobjectcontext executefetchrequest:request error:&error] mutablecopy]; if (mutablefetchresults == nil) { // handle error. } [self seteventsarray:mutablefetchresults]; [mutablefetchresults release]; [request release]; uibarbuttonitem * addbutton = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target:self action:@selector(showprompt)]; [self.navigationitem setleftbarbuttonitem:addbutton]; [addbutton release]; uibarbuttonitem *editbutton = [[uibarbuttonitem alloc]initwithtitle:@"edit" style:uibarbuttonitemstylebordered target:self action:@selector(toggleedit)]; self.navigationitem.rightbarbuttonitem = editbutton; [editbutton release]; [super viewdidload]; } - (void)viewdidunload { self.eventsarray = nil; [super viewdidunload]; } -(void)toggleedit { [self.tableview setediting: !self.tableview.editing animated:yes]; if (self.tableview.editing) [self.navigationitem.rightbarbuttonitem settitle:@"done"]; else [self.navigationitem.rightbarbuttonitem settitle:@"edit"]; } - (void)dealloc { [managedobjectcontext release]; [eventsarray release]; [entered release]; [super dealloc]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; } #pragma mark - #pragma mark add event -(void)addevent { routine *routine = (routine *)[nsentitydescription insertnewobjectforentityforname:@"routine" inmanagedobjectcontext:managedobjectcontext]; routine.name=entered; nserror *error = nil; if (![managedobjectcontext save:&error]) { // handle error. } nslog(@"%@", error); [eventsarray insertobject:routine atindex:0]; nsindexpath *indexpath = [nsindexpath indexpathforrow:0 insection:0]; [self.tableview insertrowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade]; [self.tableview scrolltorowatindexpath:[nsindexpath indexpathforrow:0 insection:0] atscrollposition:uitableviewscrollpositiontop animated:yes]; } -(void)showprompt { alertprompt *prompt = [alertprompt alloc]; prompt = [prompt initwithtitle:@"add workout day" message:@"\n \n please enter title workout day" delegate:self cancelbuttontitle:@"cancel" okbuttontitle:@"add"]; [prompt show]; [prompt release]; } - (void)alertview:(uialertview *)alertview willdismisswithbuttonindex:(nsinteger)buttonindex { if (buttonindex != [alertview cancelbuttonindex]) { entered = [(alertprompt *)alertview enteredtext]; if(eventsarray && entered) { routine *temproutine = (routine *)[nsentitydescription insertnewobjectforentityforname:@"routine" inmanagedobjectcontext:managedobjectcontext]; temproutine.name = entered; // routine *temproutine = [[routine alloc]init]; //temproutine.name = entered; [eventsarray addobject:temproutine]; [temproutine release]; [tableview reloaddata]; [self addevent]; } /* if(eventsarray && entered) { [eventsarray addobject:entered]; [tableview reloaddata]; [self addevent]; } */ } } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [eventsarray count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; // dequeue or create new cell. uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier] autorelease]; } routine *temproutine = (routine *)[eventsarray objectatindex:indexpath.row]; cell.textlabel.text = temproutine.name; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; return cell; } // override support conditional editing of table view. - (bool)tableview:(uitableview *)tableview caneditrowatindexpath:(nsindexpath *)indexpath { // return no if not want specified item editable. return yes; } -(void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { if (editingstyle == uitableviewcelleditingstyledelete) { // delete managed object @ given index path. nsmanagedobject *eventtodelete = [eventsarray objectatindex:indexpath.row]; [managedobjectcontext deleteobject:eventtodelete]; // update array , table view. [eventsarray removeobjectatindex:indexpath.row]; [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:yes]; // commit change. nserror *error = nil; if (![managedobjectcontext save:&error]) { // handle error. } } }
when create routines, create them insertnewobjectforentityforname:inmanagedobjectcontext:. release them. insertnewobjectforentityforname:inmanagedobjectcontext: doesn't return object own according memory management rules or method's documentation, shouldn't releasing.
Comments
Post a Comment