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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:10:22+00:00 2026-05-30T10:10:22+00:00

My object has a private NSMutableArray items. I am using the following code to

  • 0

My object has a private NSMutableArray items. I am using the following code to sort the objects in items in size order:

-(void)sortItems{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"size" ascending:YES];    
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [items sortedArrayUsingDescriptors:sortDescriptors];
NSMutableArray* newArray = [[NSMutableArray alloc] initWithArray: sortedArray];
[self setItems:newArray];
[sortDescriptor release];   

}

Obviously this is a memory leak here, because every time I call sortItems, I am allocing new memory and assigning items to point to it. I’ve tried releasing the old memory as follows:

NSMutableArray* newArray = [[NSMutableArray alloc] initWithArray: sortedArray];
NSMutableArray* oldArray = [self items];
[self setItems:newArray];
[oldArray release];

But that gives an EXC_BAD_ACCESS error. I’ve read up on memory handling in objC, and I’m convinced I’m doing something fundamentally wrong here.

Any help would be greatly appreciated!

  • 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-30T10:10:24+00:00Added an answer on May 30, 2026 at 10:10 am

    You’re leaking the new array, not the old one:

    NSMutableArray* newArray = [[NSMutableArray alloc] initWithArray: sortedArray];
    [self setItems:newArray];
    [sortDescriptor release];
    [newArray release]; // <-- add this
    

    The fundamental rule is that you must release anything that you have allocated, and you normally shouldn’t care about keeping things retained for anyone (i.e. [self setItems:]), those who need something retained will do it themselves.

    I would also recommend making self.items a mutable array, and using [self.items sortUsingDescriptors:sortDescriptor to sort inplace without creating a copy.

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

Sidebar

Related Questions

I know the GUI has private void Form1_Closing(object sender, System.ComponentModel.EventArgs e) { //do stuff
I have an array of objects. Each object has a property called name. I
I'm converting some Java code to Javascript, and the Java object has a Static
I have an object of class which has private constructor: class CL_GUIComponent { //
If an object has a Single Responsibility , can the following be acceptable: public
I have a domain object that has a Map: private Map<AutoHandlingSlotKey, LinkedHashSet<AutoFunction>> autoHandling; When
My roster object has the following property: [DataMember] public ObservableCollection<personDetail> personContact { get {
I have an array of objects. Each object has a color attribute which could
I have built a dom object Engine that has private/public fields/methods that I have
My code behind file receives an instance object Session which has a property AvailableCountries

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.