Reputation: 1139
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.
Upvotes: 5
Views: 618
Reputation: 1179
The underlaying reason for this is that Swift & Objective-C handle memory management for Foundation references differently.
In Objective-C, ARC does not manage the retain count of Foundation objects for you. It's the developer's job to call CFRetain/CFRelease where necessary.
In Swift the retain count is managed for you. In fact you can't call CFRetain/CFRelease at all!
When you create the gradient in Swift and assign it directly to the objective-c property no memory management is done. As there are no strong references to it, it's released (not necessary immediately).
In objective-c it will not be nil, so the check will pas, but the gradient would have been released and the memory no longer valid.
The accepted solution works because there is a strong reference - var gradient
Upvotes: 0
Reputation: 317
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.
Upvotes: 6
Reputation: 34935
One thing that I note is that in the setup code you use self.myGraph.gradientBottom
while in the drawing code you use self.bottomGradient
.
Are these supposed to be the same? Or did you forget to assign a value to the latter?
Upvotes: 0