views:

139

answers:

1

I tried everything to debug this one but I can't get to the bottom of it.

This code lives in a subclass of NSOperation which is processed from a queue:

(borders is an ivar NSArray containing 5 UIimage objects)


NSMutableArray *images = [[NSMutableArray alloc] init];

for (unsigned i = 0; i < 5; i++)
    {       

    CGSize size = CGSizeMake(60, 60);
    UIGraphicsBeginImageContext(size);

    CGPoint thumbPoint = CGPointMake(6, 6);

    [controller.image drawAtPoint:thumbPoint];

    CGPoint borderPoint = CGPointMake(0, 0);
    [[borders objectAtIndex:i] drawAtPoint:borderPoint];

    [images addObject:UIGraphicsGetImageFromCurrentImageContext()];

    UIGraphicsEndImageContext();

}

[images release];

The code works fine most of the time but when I push the iphone by access subviews and pressing lots of buttons on the UI I either get this exception which is trapped by the operation:

Exception Load view: *** -[NSCFArray insertObject:atIndex:]: attempt to insert nil

or I get this:

Program received signal:  “EXC_BAD_ACCESS”.

The exception is caused because UIGraphicsGetImageFromCurrentImageContext() return nil.

I don't know how to debug the EXC_BAD_ACCESS but I'm guessing that this error (in fact both of these errors) is caused by low memory. The debugger stops at the line:

    [controller.image drawAtPoint:thumbPoint];

As I mentioned I've trapped the exception so I can live with that but the EXC_BAD_ACCESS is more serious.

IF this is memory related how can I tell and is it possible to increase the memory available to NSOperation?

+1  A: 

Is the NSOperationQueue being processed on the main thread or a background thread? If the latter you will not be able to do any graphics context stuff (or sometimes you will sometimes you won't).

Phil Nash
It's on a background thread (well the queue is processed on the main thread but the operation runs in the background) , the code works 90% of the time, its only when I start hammering the UI that it fails. Why shouldn't I be able to do graphics context stuff on the backround thread?
Joe
CoreGraphics calls are generally ok, but UI calls (such as `UIGraphicsBeginImageContext`) are usually only callable from the main thread. I don't the finer details, but you might fine some more info here: http://www.iphonedevsdk.com/forum/iphone-sdk-development/13963-background-threads-graphics.html
Phil Nash
If you can recast your code using `CGBitmapContext` you'll probably solve your problem.
Phil Nash
You might also find a question I asked here a year ago relevant: http://stackoverflow.com/questions/702914/using-core-graphics-cocoa-can-you-draw-to-a-bitmap-context-from-a-background-th
Phil Nash
Thanks for the advice Phil. I'll give it a try.
Joe
Phil you are a genius, I've spent hours on this with no luck but using UIGraphicsBeginImageContext seems to work! More testing to do but thanks again for your help.
Joe
Did you mean, `CGBitmapContext`? You were already using `UIGraphicsBeginImageContext`. Either way - glad I could help :-)
Phil Nash
Sorry yeah CGBitmapContext, cut and paste error.
Joe