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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:13:34+00:00 2026-05-26T01:13:34+00:00

If I have an NSMutableArray of custom objects, how can I must easily clear

  • 0

If I have an NSMutableArray of custom objects, how can I must easily clear the array without causing any memory issues? Assume that the custom object class has a dealloc method in it which correctly released an instance variables etc.

For example is it ok to use the NSArray “removeAllObjects” method?

  • If yes – how does this work – does “removeAllObjects” call the “dealloc” method on each object as it removes them

  • If no – what would be the easiest approach to use?

EDIT (after 4 replies) – One last question of clarification after the great replies – I’m still not quite sure about the instance variables/properties in my custom object that I have set to retain? These seem to be only released via the the “dealloc” method in my custom object class, where we do this manually along with [super release].

So if, re clearing an array, if I do a removeAllObjects, and then NSArray issues a “release” to my custom objects, but doesn’t call “dealloc”, then how do my instance variables get released?

  • 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-26T01:13:35+00:00Added an answer on May 26, 2026 at 1:13 am

    removeAllObjects will remove the object from the array. This process will send a release message to the object and this will decrease its reference count. When the reference count reaches zero the object will be deallocated.

    don’t do it like this, because it will leak.

    NSObject *object = [[NSObject alloc] init];       + 1
    [array addObject:object];                         + 1
    [array removeAllObjects];                         - 1
                                                    =======
                                                    = + 1 -> Leak
    

    this is the correct way:

    NSObject *object = [[[NSObject alloc] init] autorelease]; + 1 (from alloc) - 1 (from autorelease)
    [array addObject:object];                         + 1
    [array removeAllObjects];                         - 1
                                                    =======
                                                    =   0 -> Object will be deallocated
    

    Instead of calling removeAllObjects you could just release the array. If an array is deallocated everything that’s inside of it gets released and if there is no other reference to the object it will be deallocated.

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

Sidebar

Related Questions

I have an NSMutableArray of custom objects. The custom object (Tag Class) has an
I have a NSMutableArray that has custom objects in it. Each object does have
Possible Duplicate: How to sort an NSMutableArray with custom objects in it? i have
I have an NSMutableArray of custom objects with various information them. For instance the
I have an NSMutableArray that contains a few custom objects. Two of the objects
I have a custom object and NSMutableArray as instance member. I fill the array
I have an NSMutableArray that is populated with objects of strings. For simplicity sake
I have an NSMutableArray. It's members eventually become members of an array instance in
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
I have an object that has an NSMutableArray of custom UIViews, a UIImage, and

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.