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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:53:45+00:00 2026-05-16T03:53:45+00:00

I hate asking memory management questions – all the good answers are variations on

  • 0

I hate asking memory management questions – all the good answers
are variations on RTFM. But this one is stumping me.

I have a (relatively) complex dictionary in my model class,
where each key points to an array of arrays. I constantly add and delete
items to it, depending on state. Each “item” is an array.

- (void)addToDictionary:(NSNumber *)itemID {

        // get what we need (associated array of arrays & key) from the incoming ID
        NSArray *incomingArray = [self getArrayFromID:[itemID intValue]];
        NSString *myKey = [incomingArray objectAtIndex:0];

        NSMutableArray *myNewArray = [[NSMutableArray alloc] init];

        // case 1: this key is not in the dictionary yet
        if ([[myDict allKeys] containsObject:myKey] == NO) {
            [myNewArray addObject:incomingArray];
            [myDict setObject:myNewArray forKey:myKey];

        // case 2: key already there; add new array to its array
        } else {
            myNewArray = [NSMutableArray arrayWithArray:[myDict objectForKey:myKey]];
            [myNewArray addObject:incomingArray];
            [myDict removeObjectForKey:myKey];
            [myDict setObject:myNewArray forKey:myKey];
        }
        // why isn't this line working??
        [myNewArray release];
}

My question is the last line. I allocated this array to help me
work with the dictionary, and now I don’t need it any more.
But the program will crash if I release it, and work just fine
if I comment that line out. What am I missing?
TIA

  • 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-16T03:53:46+00:00Added an answer on May 16, 2026 at 3:53 am

    In case two you don’t own the returned array. So only release it in case 1. And don’t create something you’ll not use. The NSMutableArray pointer will get assigned to some other data in case 2, not the one you’ve allocated. So you can’t release something you don’t own.

    - (void)addToDictionary:(NSNumber *)itemID {
        NSArray *incomingArray = [self getArrayFromID:[itemID intValue]];
        NSString *myKey = [incomingArray objectAtIndex:0];
    
        NSMutableArray *myNewArray;
    
        if ([[myDict allKeys] containsObject:myKey] == NO) {
            // Create when you need it
            myNewArray = [[NSMutableArray alloc] init];
            [myNewArray addObject:incomingArray];
            [myDict setObject:myNewArray forKey:myKey];
    
            // release when you're done with it
            [myNewArray release];
    
        } else {
            myNewArray = [NSMutableArray arrayWithArray:[myDict objectForKey:myKey]]; // you don't own it!
            [myNewArray addObject:incomingArray];
            [myDict removeObjectForKey:myKey];
            [myDict setObject:myNewArray forKey:myKey];
        }
        // why isn't this line working??
        //[myNewArray release];
        // because in case 2 it's not pointing to the right memory
    }
    

    Hope it works,
    ief2

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

Sidebar

Related Questions

I hate asking questions like this - they're so undefined... and undefinable, but here
Sorry guys, I hate asking dumb questions but I have seriously been searching for
query = Files .Where(file => file.Fileinfo.Name.ToUpper().Contains(textBox1.Text.ToUpper())) .Take(7).ToList(); I hate asking this question, but I
I found a lot of questions asking for the best tool, but none asking
I hate case sensitivity in databases, but I'm developing for a client who uses
I hate Physics, but I love software development. When I go back to school
I hate to have to ask, but I'm pretty stuck here. I need to
I work for a CMMI level 5 certified company and one thing I hate
I hate the JavaBeans pattern with a passion that burns like the fire of
I really hate sometimes how IDictionary<TKey, TValue> [key] will throw an exception if the

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.