Best practices to avoid memory leaks for shinobi charts in UITableViewCell


#1

Dear Shinobi community, we’ve been using Shinobi charts in our app for the last 10+ months and it just works great and our users love it. Over the last couple of months, there were few changes to our app:

  1. iOS 9.x upgrade 2) Shinobi charts upgrade to 2.8.5 (from 2.6.0)

And our app is now crashing after the controller that displays the charts is loaded few times. There is about 50MB of memory being retained on every load. While I am going through Instruments tutorials and looking at other options, I don’t have a handle on this one yet.

Our code has the following logic inside cellForRowAtIndex:

CustomCell* cell = [tableview dequeueReusableCellWithIdentifier:@“customCell”];

chart = [[ShinobiChart alloc] initWithFrame:<frame>];

[self setChartStyles:chart];

[chart removeFromSuperView]; // added this after reading on one of the previous posts, but has no effect

[cell.contentView addSubview:chart];

What could be wrong in here that causes the leak? Thanks for your help!


#2

The same with this issue.
I add a candlestick chart in UITableViewCell and It looks like the memory will be not destroyped.


#3

Hi,

Sorry it’s taken us a while to get back to you.

Although it’s hard to tell without seeing your code, it may be that you’re creating multiple instances of the chart in each cell: i.e. when a cell is dequeued and reused, rather than reusing the chart that’s already been created, you’re creating a new chart and adding that to the cell in addition to the pre-existing chart. This means the cell will to continue to have charts added to it, therefore you need to make sure you remove the previous instance of the chart. From the looks of your sample code, you are calling [chart removeFromSuperview] on the chart you’ve just created, not the pre-existing chart.

A good way to ensure you’re not creating multiple charts for the same cell is to create a custom cell with a chart property that is instantiated once:

@interface ChartCell : UITableViewCell

@property (strong, nonatomic) ShinobiChart *chart;

@end

@implementation ChartCell

- (ShinobiChart *)chart
{
if (!_chart)
{
     _chart = [[ShinobiChart alloc] initWithFrame:self.bounds];
}

return _chart;
}

@end

I’d also recommend taking a look at this github project (it’s a collection view rather than a table view, however the principles are very similar - each cell has a single chart where the datasource is set when a cell is dequeued).

If you’re still having trouble, please feel free to send a sample in to info@shinobicontrols.com  and we’ll take a look.

Thanks,

Sam