代码之家  ›  专栏  ›  技术社区  ›  Kristof Van Landschoot

使用NSImage时垃圾收集崩溃

  •  1
  • Kristof Van Landschoot  · 技术社区  · 17 年前

    这段代码是从我正在进行的一个项目中分离出来的。它在我的Mac OS 10.5.7上持续再现垃圾收集错误,有时还会崩溃。我已经研究它太久了,所以我的问题是:其他人看到为什么在垃圾收集时会出现错误了吗?

    - (void) doCrash: (id) sender
    {
        NSArray *lURLArray = [ NSArray arrayWithObjects:
                              @"http://userserve-ak.last.fm/serve/300x300/23621007.jpg",
                              @"http://userserve-ak.last.fm/serve/300x300/26675609.png",
                              @"http://userserve-ak.last.fm/serve/300x300/26675609.png",
                              nil ];
        NSString *lImageURL = nil;
        for (lImageURL in lURLArray)
        {
            NSImage *lImage = [[NSImage alloc] initWithContentsOfURL: [NSURL URLWithString: lImageURL]];
    
            NSSize targetSize = NSMakeSize(80,80);
            NSImage *newImage = [[NSImage alloc] initWithSize:targetSize];
            [newImage lockFocus];
    
            NSRect thumbnailRect = NSMakeRect(0,0,80,80);
            NSRect sourceRect = NSMakeRect(0,0,[lImage size].width,[lImage size].height);
    
            [lImage drawInRect: thumbnailRect
                      fromRect: sourceRect
                     operation: NSCompositeSourceOver
                      fraction: 1.0];                
    
            [newImage unlockFocus];
        }
    }
    

    当垃圾回收器释放其中一个映像时,会触发垃圾回收错误消息,如下所示:

    reference count underflow for <address>, break on auto_refcount_underflow_error to debug.
    

    谢谢,

    1 回复  |  直到 17 年前
        1
  •  2
  •   Kristof Van Landschoot    17 年前

    苹果公司的某个人向我证实了这是OX X 10.5.7中的一个错误。

    rdar://problem/6938657