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 7434963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:58:46+00:00 2026-05-29T09:58:46+00:00

Quick question: I use lots of NSObject derived classes and am wondering how to

  • 0

Quick question: I use lots of NSObject derived classes and am wondering how to properly cleanup class properties that may own instances of other classes (in the snippet below this is an array of custom class instances). Is my usage of new and finalize below correct?

My understanding is that new is a convenience method that calls both alloc and init, and finalize gets called before dealloc – at least this is what I have gleaned from reading the docs. Do I have this right?

Thanks for any tips/best practices, etc!

- (id)new {
waffleArray = [[NSMutableArray alloc] initWithCapacity:kCellCount];

    for (int i = 0; i < kCellCount;  i++) {
        WaffleCell * cell = [WaffleCell new];
        [waffleArray addObject:cell];
     }

    return self;
}

// clean up
- (void)finalize {

    [waffleArray removeAllObjects];
     waffleArray = nil;

    [super finalize];
}
  • 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-29T09:58:48+00:00Added an answer on May 29, 2026 at 9:58 am

    new on NSObject is a class method, not an instance method as you have it. Also I don’t really see why you’d overload new. It would be more common to overload init so something like this:

    - (id)init {
        if ((self = [super init])) {
            waffleArray = [[NSMutableArray alloc] initWithCapacity:kCellCount];
    
            for (int i = 0; i < kCellCount;  i++) {
                WaffleCell * cell = [WaffleCell new];
                [waffleArray addObject:cell];
            }
        }
        return self;
    }
    

    As for finalize, you really don’t need to do that. This is what Apple says about it:

    The garbage collector invokes this method on the receiver before disposing of the memory it uses. When garbage collection is enabled, this method is invoked instead of dealloc.

    With ARC enabled you wouldn’t need to do anything and since the garbage collector will not be running anyway, finalize won’t get called anyway. ARC will automatically generate code which will release waffleArray in dealloc for you, which is enough for proper memory management in this case because waffleArray‘s retain count will then drop to 0, be deallocated itself which will go and release the objects in the array.

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

Sidebar

Related Questions

Quick question. I have an app that use a native DLL through PInvoke, this
Quick question, I was wondering if there is a way to use Solr's API
Quick question: If I have a very large function/sub in a class that is
Hey quick question, I am wondering Do big sites use php's getenv function to
A quick question. I want to use a php file that will deliver pages
A quick question about best practice with PHP classes. I have seen people use
Quick question. What do you think, I have a few sites that use a
Just a quick question, I'm having a little difficulty understanding where to use properties
Quick question: When do you decide to use properties (in C#) and when do
Quick Question: Is it possible to use .netCharting with MVC by streaming data from

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.