Marsson
Marsson

Reputation: 685

updating entity using Core Data sending message to deallocated instance

I am experiencing a most weird error trying to update values on entities. I a new to core data, so might be something stupid...

here is my update code:

-(BOOL)trataTarefa:(TarefaMap*) mapa{

NSFetchRequest *request =[[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [[DataSource sharedManager]managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tarefa" inManagedObjectContext:context];
[request setEntity:entity];
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"cd_tarefa == %@ AND cd_instalacao contains[cd] %@ ", mapa.cd_tarefa, mapa.cd_instalacao];
[request setPredicate:predicate];

NSError *error=nil;
NSArray *result = [context executeFetchRequest:request error:&error];
[request release];
NSLog(@"result=%d",[result count]);

if([result count]==1){
    if (mapa.tp_operacao ==3){

        Tarefa* tarefa = [result objectAtIndex:0];
        [tarefa setTp_operacao:[NSNumber numberWithInt:3]]; 


        NSLog(@"Tarefa=%@", tarefa);
        NSError* saveError=nil;
        if (![context save:&saveError]) {
            NSLog(@"Saving changes to book book two failed: %@", saveError);
        } else {
            NSLog(@"Teoricamente gravou...") ;   

        }            
    }
}    
return YES;

}

the code works, it saves the entries, but it returns a * -[CFNumber release]: message sent to deallocated instance ##### tracing it with shell malloc_history, I get:

Date/Time:       2011-03-24 14:12:01.988 -0300
OS Version:      Mac OS X 10.6.6 (10J567)
Report Version:  7

ALLOC 0x6603f60-0x6603f6f [size=16]: thread_a052f540 |start | main | UIApplicationMain |
-[UIApplication _run] | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun |  
 __CFRunLoopDoSource1 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ | 
 PurpleEventCallback | _UIApplicationHandleEvent | -[UIApplication sendEvent:] 
| -  [UIApplication handleEvent:withNewEvent:] | -[UIApplication
 _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] | -
[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:] | -[UIStatusBar
requestStyle:animated:] | -[UIStatusBar requestStyle:animationParameters:] | -  
[UIStatusBarWindow setCornerStyle:] | -[UIImageView initWithImage:] | -[UIImageView 
setImage:] | -[UIImageView(UIImageViewInternal) _updateState] | -
[UIImageView(UIImageViewInternal) _canDrawContent] | -[UIView(Rendering) contentMode] |
+[NSNumber numberWithInt:] | -[NSPlaceholderNumber initWithInt:] | CFNumberCreate |
_CFRuntimeCreateInstance | malloc_zone_malloc 

Binary Images:
0x1000 -    0x5cff3 +Nonononono (??? - ???) <C4B33680-F374-3E37-9C91-6DEDE06C4701> 
 /Users/marsson/Library/Application Support/iPhone Simulator/4.1/Applications/B13321A3-
 1E73-4DC0-8E37-E2E56116ECEA/Nonononoo.app/Nnonononononono

What I can take from that log, is that the object is not really deallocated... I already tried to update the object with setValue:forKey with the same result... if I comment the line on the update, the code runs, but of course the object is not updated. Anyone had a similar problem??

Thanks in advance!

Upvotes: 4

Views: 964

Answers (2)

Marsson
Marsson

Reputation: 685

Actually, it was a stupid thread problem... I was trying to update the ManagedContext from an background thread, and it was sometimes crashing the application. Once I used a different managed context, and used notification center to sync the context, everything went smooth. Thanks everyone for the suggestions...

Upvotes: 1

Dave Kammeyer
Dave Kammeyer

Reputation: 369

Looks like a problem with [tarefa setTp_operacao:]. Can you post the code to the Tarefa class? The NSNumber you create is autoreleased, so if Tarefa is not retaining it when setTP_operacao is run, you will be accessing a deallocated object after the autorelease pool releases (probably at the end of the current UI event).

Upvotes: 0

Related Questions