Search
Rich's Mad Rants
Powered by Squarespace

Creating iOS 5 Apps Discussion > GravityScribbler fix

Hello
I could not get GravityScribbler (Chapter 8) to work until I edited out

// UIGraphicsEndImageContext();

toward the end of setFrame method in Canvas class. Then it worked OK. Is this correct? I am running the app on an IPad2.

Thanks
DaveK

April 9, 2012 | Unregistered CommenterDaveK

I should have given a little more info. Otherwise I get a problem around

fullImage = CGBitmapContextCreateImage(self.imageContext);

in drawSketchToContext in the Canvas class.

Specifically I get invalid context errors.

I am curious if editing this line out will create a memory leak or other problems.

Thanks
DaveK

Thanks Dave

April 9, 2012 | Unregistered CommenterDaveK

Deleting UIGraphicsEndImageContext() should fix the problem, but will also create memory leaks (it should release the context that was created by the UIGraphicsBeginImageContextWithOptions() method.

However, you're correct there is a bug here. We need to retain the context before we assign it to our instance variable.

The correct code should be as follows (inside Canvas setFrame:):



dispatch_sync(self.serialQueue, ^{

CGContextRetain(context);
CGContextRelease(self.imageContext);

self.imageContext = context;
});

The really odd thing is, I had this in at one point. I must have mistakenly removed it when I converted the code from manual memory management to ARC, and I guess it worked fine under iOS 5.0.

-Rich-

April 23, 2012 | Registered CommenterRichard Warren

I've updated the errata and the source code for this bug.

April 23, 2012 | Registered CommenterRichard Warren

OK, thanks for the update.

Dave K

May 11, 2012 | Unregistered CommenterDave K