I have scoured the internet and these forums. I have examined the GalleryPlot sample code. And I’m still having trouble with this.
I am using Core Plot to draw scatterplot graphs in my iPhone application. The graphs draw beautifully.
I have done the following:
- added the ability to touch a plot point by adding the CPTScatterPlotDatasource and the CPTScatterPlotDelegate protocols to my View Controller class.
- implemented -(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index {} in my view controller class
NSLog statements show that the method is being called and the index being sent is a valid integer.
If I add any code that accesses the NSArray containing the data for the plot, the app crashes with an EXC_BAD_ACCESS. The data array is a member variable of the view controller where I implement the method.
I have tried reloading the data just before I attempt to access the array, but that didn’t solve it.
Here is my Delegate Method:
- (void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index {
NSLog(@"Touched by me: index = %d", index);
[myGraph reloadDataIfNeeded];
NSNumber* x = [[dataForOurBabyPlot objectAtIndex:index]valueForKey:@"x"];
}
The crash occurs on the “NSNumber* x” line.
Here is my View Controller class declaration:
@interface GraphView : UIViewController <CPTScatterPlotDataSource,
CPTScatterPlotDelegate> {
IBOutlet CPTGraphHostingView* myHostingView;
CPTXYGraph* myGraph;
NSArray* dataForOurBabyPlot;
}
@property (nonatomic, retain) IBOutlet CPTGraphHostingView* myHostingView;
@property (nonatomic, retain) NSArray* dataForOurBabyPlot;
// class methods
@end
What am I missing?
The call to
-reloadDataIfNeededis unnecessary—by the time you get to this point, Core Plot already has all of the data it needs.The EXC_BAD_ACCESS crash is caused by
dataForOurBabyPlotpointing to bad data. It’s likely that it got released and the system reused that memory for something else. Check your memory management and make sure that the array isn’t released until you’re done with it.