开发者

IOS : BAD ACCESS when trying to add a new Entity object

So i'm using coredata to model my relationships .

This is the model in brief

Type A can have one or more types of type B

Type B has a inverse relationship of being associated with one of type A

Type B can have one or more types of type C Type C has a inverse relationship of being associated with one of type B

From a UI standpoint , I have a Navigation controller with controllers that successively sets up the first A object (VC-1) , then another viewcontroller (VC-2) creates a B object ( I pass in the A object to this controller) and the B object is added to the A object . Similarly the same thing happens with B and C . The third Viewcontroller (VC3) first creates a C object and assigns it to the passed B Object .

Also between these viewcontrollers the managedObjectCOntext is also passed .

SO my use case is such that while viewcontroller (VC-3) is the top controller a button action will keep creating multiple objects of type C and add them to the same type B object that was passed . Also as part of this function I save the managedObject context after saving each type C .

e.g. code in viewcontroller 3

- (void) SaveNewTypeC
{
    TypeC *newtypeC =  (Question*)[NSEntityDescription insertNewObjectForEntityForName:@"TypeC" inManagedObjectContext:managedObjectContext];

    [newtypeC setProp1:] ;
        [newtypeC setProp2:] 

         ..
         .. 

    // setting up the inverse relationship here . 
    [newtypeC setBTypeObject: typeBObject];
    // adding another typeC object to the single typeB object we own here 
    [typeBObject addTypeCInTypeBObject:newtypeC];


    [section setTotalCObjectCount:[ NSNumber numberWithInt:typeCIndex++]];


    NSError *error = nil;
    if (![managedObjectContext save:&error]) {
        // Handle error
        NSLog(@"Unresolved error %@, %@, %@", error, [error userInfo],[error localizedDescription]);
        abort();  // Fail
    }

    [newtypeC release];



}

- (IBAction)selectedNewButton:(id)sender {


    [self SaveNewTypeC];

    [self startRepeatingTimer];

}

The BAD ACCESS seems to appear above where

// setting up the inverse relationshi开发者_StackOverflow中文版p here . 
        [newtypeC setBTypeObject: typeBObject];
        // adding another typeC object to the single typeB object we own here 
        [typeBObject addTypeCInTypeBObject:newtypeC];

Any clues on resolving this would be helpful .


[newtypeC release]; is the reason of BAD ACCESS. why are you releasing newtypeC object. You did not used alloc, new, retain or copy for creating this object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜