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

  • SEARCH
  • Home
  • 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 7840535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:49:14+00:00 2026-06-02T15:49:14+00:00

I am learning Objective-C and using the Big Nerd Ranch’s Obj-C book to familiarize

  • 0

I am learning Objective-C and using the Big Nerd Ranch’s Obj-C book to familiarize myself with the language. Everything is going good so far until I reach this one exercise where I am supposed to create 2 classes where each of them inherits the dealloc method. The goal to show when an object actually gets deallocated. So the new dealloc looks like

Asset.h

#import <Foundation/Foundation.h>
@class Employee;

@interface Asset : NSObject{
    NSString * label;
    unsigned int resaleValue;
}

@property (strong) NSString * label;
@property unsigned int resaleValue;

@end

Employee.h

#import "Person.h"
@class Asset;

@interface Employee : Person
{
    int employeeID;   
    NSString * lastName;
    Person * spouse;
    NSMutableArray * children;
    NSMutableArray * assets;
}

@property int employeeID;
- (void) addAssetObject: (Asset *) a;
- (unsigned int) valueOfAssets;

@end

Implementations

@implementation Employee
//omitted
    -(void) dealloc
    {
        NSLog(@"deallocating %@", self);
    }
@end


@implementation Asset
//omitted
    -(void) dealloc
    {
        NSLog(@"deallocating %@", self);
    }
@end

main.m

int main (int argc, const char * argv[])
{

    @autoreleasepool {

        NSMutableArray * employees = [[NSMutableArray alloc] init];
        for(int i = 0; i< 10; i++){
            Employee * person = [[Employee alloc] init];
            [person setWeightInKilos:90+i];
            [person setHeightInMeters:1.8 - i/10.0];
            [person setEmployeeID:i];
            [employees addObject:person];
        }

        for(int i = 0; i< 10; i++){
            Asset * asset = [[Asset alloc] init];
            NSString * currentLabel = [NSString stringWithFormat:@"Laptop %d", i];
            [asset setLabel:currentLabel];
            [asset setResaleValue:i*17];

            NSUInteger * randomIndex = random() % [employees count];
            Employee * randomEmployee = [employees objectAtIndex:randomIndex];

            [randomEmployee addAssetObject:asset];
        }

        NSLog(@"Employees: %@", employees);
        NSLog(@"Giving up ownership of one employee");
        [employees removeObjectAtIndex:5]; // This is supposed to trigger the deallocate method
        NSLog(@"Giving up ownership of array");
        employees = nil;
    }
    return 0;
}

Of course description is already inherited so that %@would work. However, when I ran it. The dealloc is not getting called and I am not getting the print outs that deallocates the object. I am not sure what I am doing wrong here.

Sidenote: Shouldn’t the dealloc do [super dealloc] as well?

  • 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-06-02T15:49:15+00:00Added an answer on June 2, 2026 at 3:49 pm

    From reading your code, I believe you’re using Automatic Reference Counting (ARC). If you’re not, you should start. (ARC is the new default, Apple’s recommendation, and clearly the future.)

    You have a circular reference: Your employee has an array of assets, which results in the assets being held in existence by the employee. Meanwhile each asset contains a strong reference to the owning holder employee. This means neither object can ever be released.

    To fix this, convert the reference from the asset back to the holding Employee to be a weak reference.

    @interface Asset : NSObject{
        NSString * label;
        __weak Employee * holder;
        unsigned int resaleValue;
    }
    

    Without this fix:

    • Employee holds assets in memory.
    • Assets hold employee in memory.
    • Even if all other references to the employee and assets disappear, they’ll continue to hold each other in memory.

    With this fix:

    • Employee holds assets in memory.
    • Assets do not hold employee in memory. Rather, they contain a safe, zeroing reference.
    • The employee will be released when no longer needed by anything else.
    • The holder instance variable will be “magically” cleared when the employee is released.
    • Since the assets are no longer referenced, they too will pass.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just starting climbing the Objective C learning curve (using Nerd Ranch iOS
I am learning Objective-C using Stephen Kochan's excellent book Programming in Objective-C 2.0. I
I've been learning Objective-C and just recently started using classes (instead of having everything
I am learning objective-c and XCode using Stephen Kochan's book. For one of the
I'm learning Objective-C using GNUstep(because I use Linux). I was thinking in create a
I am currently learning objective-c from a book. In one example, before the class
I'm learning Objective-C using GNUstep, because I don't have a Mac and the GNUstep
I've been learning Objective C lately, and I came across some code for using
I'm learning Objective-C using GNUstep, but i searched at Google for some example of
I've been learning Objective-C and Cocoa by working my way through the Hillegass book

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.