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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:28:37+00:00 2026-05-28T05:28:37+00:00

I’ve had a very rarely occurring bug where a key in an NSMutableDictionary instance

  • 0

I’ve had a very rarely occurring bug where a key in an NSMutableDictionary instance ends up pointing to the wrong object (another object in the same dictionary).

Could “key confusion” be the result of a race condition?

Here’s the code (I’ve put in new thread safety since):

- (NSNumber*) makeHashFromResizedImage:(UIImage*)original newSize:(CGSize)newSize {
    int retVal = original.hash + newSize.width * 2 + newSize.height * 4;
    return [NSNumber numberWithInteger:retVal];
}

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    NSNumber *key = [self makeHashFromResizedImage:image newSize:newSize];

    if ([self.resizedImages objectForKey:key]) {
        return [self.resizedImages objectForKey:key];
    }

    UIImage *newImage = {{image-resizing-code-here}};

    [self.resizedImages setObject:newImage forKey:key];

    return newImage;
}

Note: {{image-resizing-code-here}} was removed for brevity. I’m beginning to suspect my makeHashFromResizedImage as it probably doesn’t work as expected. Going to Unit Test it now.

  • 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-28T05:28:37+00:00Added an answer on May 28, 2026 at 5:28 am

    This:

    if ([self.resizedImages objectForKey:key]) {
        return [self.resizedImages objectForKey:key];
    }
    

    If another thread called setObject:forKey: with that key the result of the 2nd line will be different from the first line. Why not just store it in a variable? At least the thing you check and the thing you return will be the same.

    UIImage* image = [[[[self.resizedImages objectForKey:key] retain] autorelease];
    if (image) {
        return image;
    }
    

    (The retain + autorelease is just to retain image won’t be -dealloc-ed when this thread is running.)

    If you have to ensure that the key corresponding to that image will not be overwritten before the function returns, you’ll need to use a lock, as others answered.


    Edit: Perhaps you should use an NSMutableSet of the UIImage instead. Especially, don’t use a hash value as a key! A hash value does not need to be different when the two objects are not the same. Create a new class X which contains a CGSize and UIImage property, and use a NSMutableSet of X, if the same UIImage can be added multiple times.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I used javascript for loading a picture on my website depending on which small
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.