Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4572546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:39:40+00:00 2026-05-21T19:39:40+00:00

Person.h #import @interface Person : NSObject { NSString *name; int age; Person *spouse; }

  • 0

Person.h
#import

@interface Person : NSObject {
    NSString *name;
    int age;
    Person *spouse;
}

@property (nonatomic, retain) NSString* name;
@property int age;
@property (nonatomic, retain) Person *spouse;

- (id) initWithName:(NSString *)n age:(int)a;
- (id) initWithName:(NSString *)n age:(int)a spouse:(Person*)s;

@end

Person.m

@implementation Person

@synthesize name;
@synthesize age;
@synthesize spouse;

-(id) initWithName:(NSString *)n age:(int)a
{
    return [self initWithName:n age:a spouse:nil];
}

-(id) initWithName:(NSString *)n age:(int)a spouse:(Person *)s
{
    if((self = [super init]))
    {
        self.name = n;
        self.age = a;
        self.spouse = s;
    }
    return self;
}

-(NSString *)description
{
    return [NSString stringWithFormat:@"name=%@, age=%d, spouse=%@", self.name, self.age, self.spouse.name];
}

- (void) dealloc
{
    [name release];
    [spouse release];
    [super dealloc];
}

@end

Then I have code like this

Person *person1 = [[Person alloc] initWithName:@"Matt" age:33];
Person *person2 = [[Person alloc] initWithName:@"Clair" age:29 spouse:person1];
person1.spouse = person2;
NSLog(@"%@", person1);
NSLog(@"%@", person2);
NSLog(@"person1.retainCount=%d, person2.retainCount=%d", person1.retainCount, person2.retainCount);

[person2 release];
NSLog(@"person1.retainCount=%d, person2.retainCount=%d", person1.retainCount, person2.retainCount);
[person1 release];
NSLog(@"person1.retainCount=%d, person2.retainCount=%d", person1.retainCount, person2.retainCount);

At last, person1.retainCount=1 person2.retainCount=1, they never get -(void) dealloc, so when this situation comes up, how to deal with…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T19:39:41+00:00Added an answer on May 21, 2026 at 7:39 pm

    What you have here is called a retain cycle and it is the biggest flaw with a reference counting memory management scheme. This is why the convention that delegates are not retained exists.

    You need to find a way to either break the cycle (e.g. before releasing a person, set their spouse to nil, and possibly their spouse’s spouse) or avoid it altogether. You could, for instance, create a marriage class with two assign properties, one for each partner, and instead of a spouse property, each person has a retain marriage property pointing to the marriage object. Then when a person is deallocated (and it will be now), you set the marriage property for the other partner to nil.

    So your interfaces will look something like this:

    @interface Person
     // ...
    
    @property (retain) Marriage* marriage;
    @property (readonly, retain) Person* partner;
    @end
    
    @interface Marriage
    
    /*
     * DOES NOT RETAIN THE TWO PASSED IN PEOPLE
     */
    -(id) initWithPartner: (Person*) aPerson andSpouse: (Person*) anotherPerson; 
    
    /*
     *  Returns the other person in the marriage or nil if the passed in person is not in
     *  this marriage
     */
    -(Person*) partnerOf: (Person*) aPerson;
    /*
     *  Removes a person from a marriage.
     */
    -(void) removePartner: (Person*) aPerson;
    
    @end
    

    Person’s dealloc would look something like this.

    -(void) dealloc
    {
        Person* thePartner = [self partner];
    
    /*
     *  Remove both partners from the marriage so that if something else has retained it, there will not be a problem
     *  with dangling pointers
     */
        [[self marriage] removePartner: thePartner];
        [[self marriage] removePartner: self];
        [[self partner] setMarriage: nil]; // anulls the marriage
        [self setMarriage: nil];           // Assuming only self and our partner owned the marriage, it will now be gone  
        [super dealloc];
    }
    

    Person’s partner property is implemented thus:

    -(Person*) partner
    {
        return [[self marriage] partnerOf: self];
    }
    

    You could also do it the other way around so that the marriage owns the people and the people have a weak reference to the marriage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This test will fail: #import GTMSenTestCase.h @interface Person : NSObject @property (readonly) NSString *name;
I created a simple Person.h class: #import <Foundation/Foundation.h> @interface Person : NSObject { int
Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it
create table person ( name varchar(15), attr1 varchar(15), attr2 varchar(1), attr3 char(1), attr4 int
Given the schema PERSON { name, spouse } where PERSON.spouse is a foreign key
import java.util.*; class AddressBook { private static final int DEFAULT_SIZE = 25; private Person[]
I have a class Person i.e. the definition of Person with name and age.
I have a Person object with two constructors - one takes an int (personId),
I'm testing a Person class that models a person with name, address and date
I've created a data class name Person with an integer and a bool instance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.