Core Data : memory leak ?

Discussion in 'iOS Programming' started by namanhams, Mar 5, 2011.

  1. namanhams macrumors regular

    Joined:
    Jun 3, 2009
    #1
    I have googled around but i can not get the answer.

    So i have 2 NSManagedObject class : A and B
    A has a relationship with B, and B has an inverse relationship with A.
    So in class A, the 'B' property is retain. And the same as in class B.

    Code:
    Class A :  
    @property(nonatomic, retain) B *b;
    
    class B :
    @property(nonatomic, retain) A *a;
    
    I have to do something like this in my code :
    Code:
    aObject.b = bObject;
    bObject.a = aObject;
    
    Then how can i release them ?
     
  2. RonC macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #2
    Have you tried something as simple as the inverse - like:
    Code:
    aObject.b = nil;
    bObject.a = nil;
    [aObject release];
    [bObject release];
    The @synthesized version of the property setter looks pretty much like:
    Code:
    -(void)getA:(ClassForA *)newA
    {
        if (a) [a release];
        a = [newA retain];
    }
    
    so assigning nil results in a release to the retained object with nothing else getting retained (i.e., [nil retain]; doesn't really do anything).
     
  3. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #3
    You should set your properties to assign I think, otherwise you'll end up with a retain loop.
     

Share This Page