Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGContextDrawLinearGradient causing EXC_BAD_ACCESS

I am configuring my BEMSimpleLineGraph and have been able to do so successfully except for the linear gradient shading. After referencing this code in the provided example Obj-C project

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = {
        1.0, 1.0, 1.0, 1.0,
        1.0, 1.0, 1.0, 0.0
    };
    self.myGraph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);

and transcribing it to this in Swift:

let colorspace:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB()
        let num_locations:size_t = 2
        var locations: [CGFloat] = [0.0, 1.0]
        var components: [CGFloat] = [
            1.0, 1.0, 1.0, 1.0,
            1.0, 1.0, 1.0, 0.0
        ]

       self.myGraph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations)

everything builds correctly but throws up the EXC_BAD_ACCESS memory error in the BEMLine.m file included, stopping at this line

CGContextDrawLinearGradient(ctx, self.bottomGradient, CGPointZero, CGPointMake(0, CGRectGetMaxY(fillBottom.bounds)), 0);

I've included the obj-c bridging header, added CoreGraphics framework, enabled bottom color in the attributes pane of the respective ViewController in Storyboard, referenced Apple's Development pages to ensure correct data types of all the parameters, but am still coming up dry. In checking for error similarities, I have also realized that the same error comes up with trying to draw the top linear gradient as well. The error seems to lie in the Obj-C code trying to draw the gradient, but again I'm at a loss of what to do.

like image 447
Dane Jordan Avatar asked Mar 29 '15 18:03

Dane Jordan


1 Answers

I had the same issue when using BEMSimpleLineGraph from Swift. Luckily I found an answer on the issues page of the libraries Github:

https://github.com/Boris-Em/BEMSimpleLineGraph/issues/105

In order to solve the issue, I just declared a global gradient in the Swift class, like so:

var gradient  : CGGradient?

and just replaced the line

self.myGraph.gradientBottom = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations)

with:

self.gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations)
self.myGraph.gradientBottom = self.gradient

Apparently, the gradient will otherwise not remain allocated in memory and at the time the library needs to use it, it is no longer available.

like image 67
mjrehder Avatar answered Nov 13 '22 03:11

mjrehder