尝试使用核心绘图进行编译时获取EXC_BAD_ACCESS

时间:2011-08-09 13:23:30

标签: objective-c memory core-plot

作为目标C和xcode的初学者,我正在尝试做一个可以在那里找到的教程:http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application

我做了一些改动,所以最终会编译,所以这是我的.h(请原谅我申请的愚蠢名称):

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface yutyyutViewController : UIViewController <CPTPlotDataSource>
{
    CPTXYGraph *graph;

}

@end

这是我的.m(至少是重要部分):

#import "yutyyutViewController.h"

@implementation yutyyutViewController
- (void)viewDidLoad {
    [super viewDidLoad];
CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle];

xSquaredtPlot.identifier = @"X Squared Plot";

dataLineStyle.lineWidth = 1.0f;
dataLineStyle.lineColor = [CPTColor redColor];

xSquaredtPlot.dataLineStyle = dataLineStyle;
xSquaredtPlot.dataSource = self;

[graph addPlot:xSquaredtPlot];

我在应用程序开始运行后立即在最后三行的第一条非注释行中获得EXC_BAD_ACCESS。

虽然我是初学者,但我花了很多时间研究这个问题并且无法在互联网上找到解决方案。看起来我正在尝试访问xSquaredtPlot这是一个自动释放,这就是我得到错误的原因,但我理解的是在我的.h中保留一个属性,并在我的.m中进行合成。 BUt没有解决问题。

所以任何帮助都会很高兴,如果我错过了答案,我很抱歉,虽然它已经在论坛上了。

此致,Crafti。

1 个答案:

答案 0 :(得分:2)

问题是过时的教程。它已经超过两年了,从那时起核心情节发生了很多变化。关键步骤是您需要在-ObjC之外添加其他链接器标志。您还需要添加-all_load

Screenshot of linker flags

我浏览了教程并更新了它以使用当前版本的核心图。请注意,视图的类需要设置为CPTGraphHostingView而不是CPLayerHostingView。这是我的工作版本:

#import "CorePlotTestViewController.h"

@implementation CorePlotTestViewController

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad {
    [super viewDidLoad];

    graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds];

    CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
    hostingView.hostedGraph = graph;
    graph.paddingLeft = 20.0;
    graph.paddingTop = 20.0;
    graph.paddingRight = 20.0;
    graph.paddingBottom = 20.0;

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-6)
                                                   length:CPTDecimalFromFloat(12)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-5) 
                                                   length:CPTDecimalFromFloat(30)];

    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineColor = [CPTColor blackColor];
    lineStyle.lineWidth = 2.0f;

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;

    axisSet.xAxis.majorIntervalLength = CPTDecimalFromString(@"5");
    axisSet.xAxis.minorTicksPerInterval = 4;
    axisSet.xAxis.majorTickLineStyle = lineStyle;
    axisSet.xAxis.minorTickLineStyle = lineStyle;
    axisSet.xAxis.axisLineStyle = lineStyle;
    axisSet.xAxis.minorTickLength = 5.0f;
    axisSet.xAxis.majorTickLength = 7.0f;
    axisSet.xAxis.labelOffset = 3.0f;

    axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"5");
    axisSet.yAxis.minorTicksPerInterval = 4;
    axisSet.yAxis.majorTickLineStyle = lineStyle;
    axisSet.yAxis.minorTickLineStyle = lineStyle;
    axisSet.yAxis.axisLineStyle = lineStyle;
    axisSet.yAxis.minorTickLength = 5.0f;
    axisSet.yAxis.majorTickLength = 7.0f;
    axisSet.yAxis.labelOffset = 3.0f;

    CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] initWithFrame:graph.bounds] autorelease];
    xSquaredPlot.identifier = @"X Squared Plot";
    CPTMutableLineStyle *plotLineStyle = [[xSquaredPlot.dataLineStyle mutableCopy] autorelease];
    plotLineStyle.lineWidth = 1.0f;
    plotLineStyle.lineColor = [CPTColor redColor];
    xSquaredPlot.dataLineStyle = plotLineStyle;
    xSquaredPlot.dataSource = self;
    [graph addPlot:xSquaredPlot];

    CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]];
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0);
    xSquaredPlot.plotSymbol = greenCirclePlotSymbol;  

    CPTScatterPlot *xInversePlot = [[[CPTScatterPlot alloc]
                                    initWithFrame:graph.bounds] autorelease];
    xInversePlot.identifier = @"X Inverse Plot";
    plotLineStyle = [[xInversePlot.dataLineStyle mutableCopy] autorelease];
    plotLineStyle.lineWidth = 1.0f;
    plotLineStyle.lineColor = [CPTColor blueColor];
    xInversePlot.dataLineStyle = plotLineStyle;
    xInversePlot.dataSource = self;
    [graph addPlot:xInversePlot];
}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return 51;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot 
                     field:(NSUInteger)fieldEnum 
               recordIndex:(NSUInteger)index 
{
    double val = (index/5.0)-5;

    if (fieldEnum == CPTScatterPlotFieldX) { 
        return [NSNumber numberWithDouble:val]; 
    }
    else { 
        if (plot.identifier == @"X Squared Plot") { 
            return [NSNumber numberWithDouble:val*val]; 
        }
        else { 
            return [NSNumber numberWithDouble:1/val]; 
        }
    }
}

@end