ChartsOrg / ChartsOrg/Charts

ChartDataEntry instance variable 'data' compile warning / memory leak for non-ARC ObjC

Open
#4,667 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
28k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

* [x] I've read, understood, and done my best to follow the [*CONTRIBUTING guidelines](https://github.com/jjatie/Charts/blob/master/CONTRIBUTING.md).

## What did you do?

0. compile
1. Using non-ARC main project in ObjC
2. Create, chart, chart data and use ChartDataEntryBase's "data" instance variable to hang my own objects for my reference, which is the utility of this variable
3. deallocate the chart

## What did you expect to happen?

no warnings and no memory leaks

## What happened?

2. the "data" attribute defined here in ChartDataEntryBase:

/// optional spot for additional data this Entry represents
@objc open var data: Any?

becomes

/// optional spot for additional data this Entry represents
@property (nonatomic) id _Nullable data;

Which generates a warning in a non-ARC project:

"Default property attribute 'assign' not appropriate for object"

3. the behavior of this code should be assignment then according to the interface, but it is not... assigning to this instance variable as such:

id myObject = /* exists from somewhere else */ ;
ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:x y:y];
long rc1 = [myObject retainCount];
entry.data = myObject;
long rc2 = [myObject retainCount];

// if behavior is assignment then rc1 == rc2, but in practice rc2 = rc1 + 1 showing this is not an assignment

4. this would be semi-ok except when the chart is deallocated, the instance variable is now considered as a weak reference, and the object held in "data" is leaked.

I was able to work around this by subclassing ChartDataEntry and declaring a similar helper object like this:

@objc public class OshiChartDataEntry : ChartDataEntry {
// explicitly indicate weak behavior
@objc weak open var helper: AnyObject?
}

This creates the correct explicit ObjC header in non-ARC and so no warning:

@property (nonatomic, weak) id _Nullable helper;

And the reference count does not go up when assigning to this variable, and as a result the helper object is not leaked.

## Charts Environment

**Charts version/Branch/Commit Number:** 4.0.0/main/4516
**Xcode version:** 13 beta
**Swift version:** Swift 5
**Platform(s) running Charts:** iPadOS 15 (Simulator)
**macOS version running Xcode:** BigSur 11.4

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.