objective c - CGContextStrokePath triggers EXC_BAD_ACCESS in iOS > 5 -


i have custom uibutton class adds gradient , gloss effect uibutton code works in ios 4 , on ios5 simulator when run on ios 5 devices gives me exception exc_bad_access , exception triggered line :

cgcontextstrokepath(context); 

any appreciated , here's code

- (void)drawrect:(cgrect)rect {     cgcontextref context = uigraphicsgetcurrentcontext();      cgfloat actualbrightness = _brightness;     if (self.selected) {         actualbrightness -= 0.10;     }         cgcolorref blackcolor = [uicolor colorwithred:0.0 green:0.0 blue:0.0 alpha:1.0].cgcolor;     cgcolorref highlightstart = [uicolor colorwithred:1.0 green:1.0 blue:1.0 alpha:0.7].cgcolor;     cgcolorref highlightstop = [uicolor colorwithred:0.0 green:0.0 blue:0.0 alpha:0.0].cgcolor;      cgcolorref outertop = [uicolor colorwithhue:_hue saturation:_saturation brightness:1.0*actualbrightness alpha:1.0].cgcolor;     cgcolorref outerbottom = [uicolor colorwithhue:_hue saturation:_saturation brightness:0.80*actualbrightness alpha:1.0].cgcolor;      cgfloat outermargin = 7.5f;     cgrect outerrect = cgrectinset(self.bounds, outermargin, outermargin);                 cgmutablepathref outerpath = createroundedrectforrect(outerrect, 6.0);      // draw gradient outer path     cgcontextsavegstate(context);     cgcontextaddpath(context, outerpath);     cgcontextclip(context);     drawlineargradient(context, outerrect, outertop, outerbottom);      cgcontextrestoregstate(context);      if (!self.selected) {          cgrect highlightrect = cgrectinset(outerrect, 1.0f, 1.0f);         cgmutablepathref highlightpath = createroundedrectforrect(highlightrect, 6.0);          cgcontextsavegstate(context);         cgcontextaddpath(context, outerpath);         cgcontextaddpath(context, highlightpath);         cgcontexteoclip(context);          drawlineargradient(context, cgrectmake(outerrect.origin.x, outerrect.origin.y, outerrect.size.width, outerrect.size.height/3), highlightstart, highlightstop);         cgcontextrestoregstate(context);          drawcurvedgloss(context, outerrect, 180);         cfrelease(highlightpath);      }     else {         //reverse non-curved gradient when pressed         cgcontextsavegstate(context);         cgcontextaddpath(context, outerpath);         cgcontextclip(context);         drawlineargloss(context, outerrect, true);               cgcontextrestoregstate(context);      }     if (!_toggled) {         //bottom highlight         cgrect highlightrect2 = cgrectinset(self.bounds, 6.5f, 6.5f);         cgmutablepathref highlightpath2 = createroundedrectforrect(highlightrect2, 6.0);          cgcontextsavegstate(context);         cgcontextsetlinewidth(context, 0.5);         cgcontextaddpath(context, highlightpath2);         cgcontextaddpath(context, outerpath);         cgcontexteoclip(context);         drawlineargradient(context, cgrectmake(self.bounds.origin.x, self.bounds.size.height-self.bounds.size.height/3, self.bounds.size.width, self.bounds.size.height/3), highlightstop, highlightstart);          cgcontextrestoregstate(context);         cfrelease(highlightpath2);     }     else {         //toggle marker         cgrect togglerect= cgrectinset(self.bounds, 5.0f, 5.0f);         cgmutablepathref togglepath= createroundedrectforrect(togglerect, 8.0);          cgcontextsavegstate(context);         cgcontextsetlinewidth(context, 3.5);         cgcontextsetstrokecolorwithcolor(context, [uicolor whitecolor].cgcolor);         cgcontextaddpath(context, togglepath);         cgcontextstrokepath(context);         cgcontextrestoregstate(context);         cfrelease(togglepath);     }     // stroke outer path     cgcontextsavegstate(context);     cgcontextsetlinewidth(context, 0.5);     cgcontextsetstrokecolorwithcolor(context, blackcolor);     cgcontextaddpath(context, outerpath);     cgcontextstrokepath(context);     cgcontextrestoregstate(context);      cfrelease(outerpath);  } 

what wanna is related ios 5 or doing else wrong ?

the crash happens because uicolors deallocated when access cgcolorrefs.

an easy way avoid use

uicolor* blackcolor = [uicolor blackcolor]; cgcontextsetstrokecolorwithcolor(context, [blackcolor cgcolor]); 

instead of

cgcolorref* blackcolor = [[uicolor blackcolor] cgcolor]; cgcontextsetstrokecolorwithcolor(context, blackcolor); 

so arc doesn't chance deallocate uicolor objects early.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -