title: NSDictionary's key value is not equal to the set key
body: <p>I am creating an image editing app for OS X. I have the following code that basically returns the window associated with a given image, and creates one if no window is created for that image yet:</p>
  
  <pre><code>+(TNRWindow*)windowForImage:(NSImage*)img{
      static NSMutableDictionary *imageMapping;
      static int uidCounter;
      if(!imageMapping){
          imageMapping = [NSMutableDictionary dictionary];
  
      }
      TNRWindow *window = [imageMapping objectForKey:img];
      if(!window){
          window = [[TNRWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 720) styleMask:(NSResizableWindowMask|NSTitledWindowMask|NSClosableWindowMask) backing:NSBackingStoreBuffered defer:NO];
          [window center];
          window.uid = [NSNumber numberWithInt:uidCounter++];
          [imageMapping setObject:window forKey:img];
      }
      return window;
  
  }
  </code></pre>
  
  <p>I've seen this method returning a new, different window at consecutive calls at it with the same <code>NSImage</code> instance. I've analyzed the code and realized that <code>[imageMapping setObject:window forKey:img];</code> doesn't set the right key. When I step over that line, it creates a key value pair, but the key is different from the <code>img</code> object.</p>
  
  <p>Here is the <code>img</code> object:</p>
  
  <pre><code>(lldb) po img
  &lt;NSImage 0x600000078fc0 Size={1311.5999999999999, 875} Reps=(
      "NSBitmapImageRep 0x6100000ba4c0 Size={1311.5999999999999, 875} 
       ColorSpace=sRGB IEC61966-2.1 colorspace BPS=16 BPP=48 
       Pixels=5465x3646 Alpha=NO Planar=NO Format=0 
       CurrentBacking=&lt;CGImageRef: 0x6180003a31e0&gt; CGImageSource=0x61000016c900"
  )&gt;
  </code></pre>
  
  <p>Here is the key right after I set the dictionary entry:</p>
  
  <pre><code>(lldb) po [[imageMapping keyEnumerator] allObjects]
  &lt;__NSArrayM 0x618000255930&gt;(
  &lt;NSImage 0x610000279440 Size={1311.5999999999999, 875} Reps=(
      "NSBitmapImageRep 0x6180000baca0 Size={1311.5999999999999, 875} 
      ColorSpace=sRGB IEC61966-2.1 colorspace BPS=16 BPP=48 
      Pixels=5465x3646 Alpha=NO Planar=NO Format=0 
      CurrentBacking=&lt;CGImageRef: 0x6180003a31e0&gt; CGImageSource=0x61000016c900"
  )&gt;
  )
  </code></pre>
  
  <p>The objects themselves, and the <code>NSBitmapImageRep</code>s are different, but the backing <code>CGImageRef</code> and <code>CGImageSource</code> are the same. The object appears to be perfectly valid, but when I call <code>[imageMapping objectForKey:img];</code> it's returning me <code>nil</code> as the image object itself is not a key in the dictionary. There are no multiple threads that are calling this method. What is exactly going on here and how can I correct this behavior?</p>