Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should valueFormatter property use weak modifier? #1873

Open
Huang-Libo opened this issue Nov 22, 2016 · 12 comments
Open

Should valueFormatter property use weak modifier? #1873

Huang-Libo opened this issue Nov 22, 2016 · 12 comments

Comments

@Huang-Libo
Copy link

Tihs property is found in Charts-Swift.h file, it's a delegate, should it use weak modifier to avoid retain cycle?

@property (nonatomic, strong) id <IChartAxisValueFormatter> _Nullable valueFormatter;
@pmairoldi
Copy link
Collaborator

Would probably make sense.

@Huang-Libo
Copy link
Author

@petester42
I found a same issue: #1854

There are three ways to solve this problem:

  1. Use weak to modify valueFormatter (I think it's better because valueFormatter is a delegate)
  2. Use weakSelf to avoid retain cycle
  3. Manually set valueFormatter to nil when leave the controller.

@liuxuan30
Copy link
Member

Where do you see it? In which class?

@kientux
Copy link

kientux commented Jan 8, 2017

@liuxuan30 It's in ChartAxisBase class. I had this problem too. I think if valueFormatter doesn't need to be strong property, it should be weak to prevent retain circle when assign it to self. It took me 2 hours to find out why my view controller didn't deallocated :(

@liuxuan30
Copy link
Member

I filed a PR for related issues.

@liuxuan30
Copy link
Member

I tested it today and found serious issues. If we apply weak, ChartsDemo won't display axis labels at all. Still investigating.

@liuxuan30
Copy link
Member

liuxuan30 commented Jan 11, 2017

Some findings:

  1. if I change it to weak, _axisValueFormatter can't be correctly initialized. The getter of valueFormatter will even return nil, though we force to create a default one, however it does not set _axisValueFormatter.

  2. changing to weak in swift code, will not impact the objective-C header file, it's still strong:

@Property (nonatomic, strong) id _Nullable valueFormatter;

You might want to check on your side to see if it's true. I use latest Xcode 8.2.1. Although it's strong, but the behavior indeed has changed to a weak fashion, causing typical issues, like assigning a local variable will be dealloc after the function ends.

Sitting down and think of this, I don't think it's a good practice to use weak regardless the problems I had above, but if we really do so, people need to retain the formatter somewhere. So using weakSelf might be a better solution here.

@Huang-Libo
Copy link
Author

@liuxuan30
Interesting...

@931743010
Copy link

use weakSelf ;Still unable to release
__weak typeof(self) weakSelf=self;
ChartXAxis *xl = _chartView.xAxis;
xl.valueFormatter = weakSelf;

@liuxuan30
Copy link
Member

@931743010 just saying 'unable to release' can't help as from the code snippet, we can't say what's wrong. We don't have your project and even don't know if you retained self anywhere else. So if you want to prove it, make a simple project to reproduce.

@aelam
Copy link
Contributor

aelam commented Mar 17, 2017 via email

@liuxuan30
Copy link
Member

if weakSelf indeed don't work, try use a separate class as the formatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants