Crash in lockFocus?

  • I've got code that makes a small thumbnail of another image by
    creating a new NSImage, lockFocus on it, and then drawing, unlocking,
    and autoreleasing it:

    NSImage *retval = [[NSImage alloc] initWithSize:newSize];
    [retval setCacheMode:NSImageCacheNever];
    [retval lockFocus];
    [preview drawInRect:NSMakeRect(0,0,newSize.width,newSize.height)
             fromRect:NSMakeRect(0,0,curSize.width, curSize.height)
             operation:NSCompositeCopy
             fraction:1.0];
    [retval unlockFocus];
    return [retval autorelease];

    Unfortunately, occasionally the lockFocus will crash:
    Program received signal:  “EXC_BAD_ACCESS”.
    [Switching to process 19156 thread 0x3f23]
    (gdb) bt
    #0  0xfffeff10 in objc_msgSend_rtp ()
    #1  0x9141bc20 in _NXAllocateImageCache ()
    #2  0x9141b31c in -[NSCachedImageRep
    _initWithSize:depth:separate:alpha:allowDeep:] ()
    #3  0x9141b0a0 in -[NSImage lockFocus] ()
    #4  0x001330b4 in -[NSDictionary(QutilCorpse) previewOfSize:]
    (self=0x67e050, _cmd=0x174ee0, size=128) at /Volumes/Sources/qui/
    NSDictionary_QutilCorpse.mm:50
    (gdb) p newSize
    $1 = {
      width = 128,
      height = 128
    }
    (gdb) po retval
    NSImage 0x687e70 Size={128, 128} Reps=(
    )

    This only rarely happens, but it does happen, and seems to only happen
    on Leopard (10.5.1) - I tried adding the NSImageCacheNever line to try
    to see if it would go away based on some searching of the archives,
    but it didn't help. I really don't see what could possibly be wrong -
    it allocates the image of the correct size, and it really doesn't seem
    like lockFocus should crash (maybe throw an exception if, for some
    reason it couldn't lock focus, but not crash).  Worse, this problem is
    very intermittent, so not a lot to test (I may see it every few days,
    sometimes a couple of times a day)

    Any ideas of how to work around this problem?

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium | flame : flame fractals & strange attractors : build,
    mutate, evolve, animate
  • Hello,

    Could this be a thread-safety problem? If you're using multiple
    threads, are you sure that you're playing by the (barely documented)
    rules?

    j o a r

    On Jan 13, 2008, at 11:52 AM, glenn andreas wrote:

    > I've got code that makes a small thumbnail of another image by
    > creating a new NSImage, lockFocus on it, and then drawing,
    > unlocking, and autoreleasing it:
    >
    > NSImage *retval = [[NSImage alloc] initWithSize:newSize];
    > [retval setCacheMode:NSImageCacheNever];
    > [retval lockFocus];
    > [preview drawInRect:NSMakeRect(0,0,newSize.width,newSize.height)
    > fromRect:NSMakeRect(0,0,curSize.width, curSize.height)
    > operation:NSCompositeCopy
    > fraction:1.0];
    > [retval unlockFocus];
    > return [retval autorelease];
    >
    >
    > Unfortunately, occasionally the lockFocus will crash:
    > Program received signal:  “EXC_BAD_ACCESS”.
    > [Switching to process 19156 thread 0x3f23]
    > (gdb) bt
    > #0  0xfffeff10 in objc_msgSend_rtp ()
    > #1  0x9141bc20 in _NXAllocateImageCache ()
    > #2  0x9141b31c in -[NSCachedImageRep
    > _initWithSize:depth:separate:alpha:allowDeep:] ()
    > #3  0x9141b0a0 in -[NSImage lockFocus] ()
    > #4  0x001330b4 in -[NSDictionary(QutilCorpse) previewOfSize:]
    > (self=0x67e050, _cmd=0x174ee0, size=128) at /Volumes/Sources/qui/
    > NSDictionary_QutilCorpse.mm:50
    > (gdb) p newSize
    > $1 = {
    > width = 128,
    > height = 128
    > }
    > (gdb) po retval
    > NSImage 0x687e70 Size={128, 128} Reps=(
    > )
    >
    >
    > This only rarely happens, but it does happen, and seems to only
    > happen on Leopard (10.5.1) - I tried adding the NSImageCacheNever
    > line to try to see if it would go away based on some searching of
    > the archives, but it didn't help. I really don't see what could
    > possibly be wrong - it allocates the image of the correct size, and
    > it really doesn't seem like lockFocus should crash (maybe throw an
    > exception if, for some reason it couldn't lock focus, but not
    > crash).  Worse, this problem is very intermittent, so not a lot to
    > test (I may see it every few days, sometimes a couple of times a day)
    >
    > Any ideas of how to work around this problem?
  • On Jan 13, 2008, at 2:06 PM, j o a r wrote:

    > Hello,
    >
    > Could this be a thread-safety problem? If you're using multiple
    > threads, are you sure that you're playing by the (barely documented)
    > rules?
    >
    > j o a r
    >

    I am using threads (a lot, actually), but this has happened in the
    main thread (it was making a custom icon while saving) when there is
    nothing drawing anything on the secondary threads (calculating things,
    yes, but nothing that involves NSImages, or even any CG stuff).

    That was my first guess, which is why I threw in the setCacheMode
    (which was more voodoo than anything).  Plus, according to the
    archives <http://lists.apple.com/archives/Cocoa-dev/2001/Jul/msg01138.html>, "you can safely lockFocus from multiple thread" (and since that's
    all I'm doing with that NSImage, I believe I'm following the barely
    documented rules).

    I could probably switch the code over to do the "create
    NSBitmapImageRep, clear it, create NSGraphicsContext & set it, set up
    parameters, draw" dance <http://lists.apple.com/archives/cocoa-dev/2007/Nov/msg01208.html> instead of the much simpler "lockFocus", which would probably avoid
    the problem, but require a lot of changes to get going...  I should
    probably just bite the bullet and start searching my code for
    lockFocus and do a wholesale purge if I can't work this out.

    [This would be one of those few times that I wish objective-c provided
    blocks like smalltalk so that you could define new control structures
    to wrap this up cleanly, but blocks would needs to be closures to be
    truly useful, and closures in a c-based language aren't practical]

    Glenn Andreas                      <gandreas...>
      <http://www.gandreas.com/> wicked fun!
    quadrium | prime : build, mutate, evolve, animate : the next
    generation of fractal art
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today