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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:14:32+00:00 2026-05-26T18:14:32+00:00

My algorithm is pretty simple. Add an item to a cache. The item is

  • 0

My algorithm is pretty simple. Add an item to a cache. The item is stored in both an NSMutableArray and an NSMutableDictionary. The array serves as a FIFO queue, where if I pass a specified maximum size, I delete the oldest items until the size is lower than the allowed maximum.

When I remove an item, I first remove if from the array, and then from the dictionary. Then I have problems because the item seems to be over-released (so says the debugger, i.e. “does not appear to point to a valid object”).

- (void) addItem:(NSData *)value forKey:(NSString *)key {
    ApiResponseCacheItem *item = [[ApiResponseCacheItem alloc] init];
    item.cacheKey = key;
    item.cacheValue = value;

    [queue addObject:item];
    [item release];
    [dictionary setObject:item forKey:key];

    size += [value length];

    while (size > kMaxCacheSize && [queue count] > 0) {
        ApiResponseCacheItem *oldestItem = [queue objectAtIndex:0];
        size -= [oldestItem.cacheValue length];
        // remove oldest item
        [queue removeObjectAtIndex:0];
        [dictionary removeObjectForKey:oldestItem.cacheKey];
    }
}

Now, I change the order: first I remove the object from the dictionary, and then from the array, leaving everything else exactly the same. Now all is fine.

while (size > kMaxCacheSize && [queue count] > 0) {
    ApiResponseCacheItem *oldestItem = [queue objectAtIndex:0];
    size -= [oldestItem.cacheValue length];
    [dictionary removeObjectForKey:oldestItem.cacheKey];
    // remove oldest item
    [queue removeObjectAtIndex:0];
}

Please explain.

  • 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-26T18:14:32+00:00Added an answer on May 26, 2026 at 6:14 pm

    So, first of all @WTP is absolutely right, and you should accept his answer. +1 to him.

    The right thing to do is not touch ‘item’ after you have released it. And certainly don’t pass it around to other unsuspecting methods after you have already released it.

    But, having said that, if you want to understand why the order made a difference as to whether you survived the bug or not, the key is to remember that NSDictionary doesn’t retain its keys, it copies them.

    So the crashing sequence was like this:

    1. You alloc/init item – Retain count +1
    2. You add it to the array – Retain count +2
    3. You release it prematurely – Retain count +1
    4. You use it as a key in the dictionary – Retain count of your object
      still +1 (the dictionary made a copy of the object for its own use)
    5. You remove it from the array – Retain count 0 – object is gone
    6. You hand a dangling pointer off to -[NSMutableDictionary
      removeObjectForKey:] and down you go.

    The lucky sequence where the bug didn’t surface:

    1. You alloc/init -> +1
    2. You add it to the array -> +2
    3. You release it -> +1
    4. You use it as a key -> +1
    5. You hand it off to removeObjectForKey: -> Still +1 and you avoided
      the crash
    6. You remove it from the array -> Retain count goes to 0

    Again, you should do what @WTP advised. This is just background info if you’re curious. Hope that helps.

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

Sidebar

Related Questions

Consider the following pretty simple C++ code: #include <algorithm> #include <iostream> using namespace std;
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
I am looking to optimize a fairly simple algorithm that is currently O(n 2
New Rails programmer here. There is probably a pretty simple solution to this, but
At the outset this looks pretty simple, however this was an interview question and
I've been working on a graph traversal algorithm over a simple network and I'd
I've been looking around trying to find a simple binomial coefficient algorithm, to no
Algorithm to generate all possible letter combinations of given string down to 2 letters
What algorithm taught you the most about programming or a specific language feature? We
Which algorithm would you use to search short substrings in short texts? By short

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.