Browse Source

Memory leak fix (#2273)

* disable bindResize Memory leak source

* add babel config to prevent rollup error

* Major memory leak fix

* unecessary comment

* run dist

* add rollup back to package.json

* remove babelrc
pull/2292/head
Nazih Ahmed 7 years ago committed by Yoshiya Hinosawa
parent
commit
45ae244dc1
  1. 31
      c3.js
  2. 2
      c3.min.js
  3. 4996
      package-lock.json
  4. 2
      package.json
  5. 7
      src/api.chart.js
  6. 10
      src/core.js

31
c3.js

@ -1960,7 +1960,7 @@ c3_chart_internal_fn.bindResize = function () {
var $$ = this, var $$ = this,
config = $$.config; config = $$.config;
$$.resizeFunction = $$.generateResize(); $$.resizeFunction = $$.generateResize(); // need to call .remove
$$.resizeFunction.add(function () { $$.resizeFunction.add(function () {
config.onresize.call($$); config.onresize.call($$);
@ -1980,10 +1980,19 @@ c3_chart_internal_fn.bindResize = function () {
config.onresized.call($$); config.onresized.call($$);
}); });
$$.resizeIfElementDisplayed = function () {
// if element not displayed skip it
if ($$.api == null || !$$.api.element.offsetParent) {
return;
}
$$.resizeFunction();
};
if (window.attachEvent) { if (window.attachEvent) {
window.attachEvent('onresize', $$.resizeFunction); window.attachEvent('onresize', $$.resizeIfElementDisplayed);
} else if (window.addEventListener) { } else if (window.addEventListener) {
window.addEventListener('resize', $$.resizeFunction, false); window.addEventListener('resize', $$.resizeIfElementDisplayed, false);
} else { } else {
// fallback to this, if this is a very old browser // fallback to this, if this is a very old browser
var wrapper = window.onresize; var wrapper = window.onresize;
@ -1997,7 +2006,14 @@ c3_chart_internal_fn.bindResize = function () {
} }
// add this graph to the wrapper, we will be removed if the user calls destroy // add this graph to the wrapper, we will be removed if the user calls destroy
wrapper.add($$.resizeFunction); wrapper.add($$.resizeFunction);
window.onresize = wrapper; window.onresize = function () {
// if element not displayed skip it
if (!$$.api.element.offsetParent) {
return;
}
wrapper();
};
} }
}; };
@ -3472,9 +3488,9 @@ c3_chart_fn.destroy = function () {
} }
if (window.detachEvent) { if (window.detachEvent) {
window.detachEvent('onresize', $$.resizeFunction); window.detachEvent('onresize', $$.resizeIfElementDisplayed);
} else if (window.removeEventListener) { } else if (window.removeEventListener) {
window.removeEventListener('resize', $$.resizeFunction); window.removeEventListener('resize', $$.resizeIfElementDisplayed);
} else { } else {
var wrapper = window.onresize; var wrapper = window.onresize;
// check if no one else removed our wrapper and remove our resizeFunction from it // check if no one else removed our wrapper and remove our resizeFunction from it
@ -3483,6 +3499,9 @@ c3_chart_fn.destroy = function () {
} }
} }
// remove the inner resize functions
$$.resizeFunction.remove();
$$.selectChart.classed('c3', false).html(""); $$.selectChart.classed('c3', false).html("");
// MEMO: this is needed because the reference of some elements will not be released, then memory leak will happen. // MEMO: this is needed because the reference of some elements will not be released, then memory leak will happen.

2
c3.min.js vendored

File diff suppressed because one or more lines are too long

4996
package-lock.json generated

File diff suppressed because it is too large Load Diff

2
package.json

@ -61,7 +61,7 @@
"node-static": "^0.7.9", "node-static": "^0.7.9",
"nodemon": "^1.11.0", "nodemon": "^1.11.0",
"rollup": "^0.55.0", "rollup": "^0.55.0",
"rollup-plugin-babel": "^3.0.0", "rollup-plugin-babel": "^3.0.3",
"uglify-js": "^3.0.15", "uglify-js": "^3.0.15",
"watchify": "^3.9.0" "watchify": "^3.9.0"
}, },

7
src/api.chart.js

@ -22,9 +22,9 @@ c3_chart_fn.destroy = function () {
} }
if (window.detachEvent) { if (window.detachEvent) {
window.detachEvent('onresize', $$.resizeFunction); window.detachEvent('onresize', $$.resizeIfElementDisplayed);
} else if (window.removeEventListener) { } else if (window.removeEventListener) {
window.removeEventListener('resize', $$.resizeFunction); window.removeEventListener('resize', $$.resizeIfElementDisplayed);
} else { } else {
var wrapper = window.onresize; var wrapper = window.onresize;
// check if no one else removed our wrapper and remove our resizeFunction from it // check if no one else removed our wrapper and remove our resizeFunction from it
@ -33,6 +33,9 @@ c3_chart_fn.destroy = function () {
} }
} }
// remove the inner resize functions
$$.resizeFunction.remove();
$$.selectChart.classed('c3', false).html(""); $$.selectChart.classed('c3', false).html("");
// MEMO: this is needed because the reference of some elements will not be released, then memory leak will happen. // MEMO: this is needed because the reference of some elements will not be released, then memory leak will happen.

10
src/core.js

@ -918,7 +918,7 @@ c3_chart_internal_fn.observeInserted = function (selection) {
c3_chart_internal_fn.bindResize = function () { c3_chart_internal_fn.bindResize = function () {
var $$ = this, config = $$.config; var $$ = this, config = $$.config;
$$.resizeFunction = $$.generateResize(); $$.resizeFunction = $$.generateResize(); // need to call .remove
$$.resizeFunction.add(function () { $$.resizeFunction.add(function () {
config.onresize.call($$); config.onresize.call($$);
@ -938,9 +938,9 @@ c3_chart_internal_fn.bindResize = function () {
config.onresized.call($$); config.onresized.call($$);
}); });
var resizeIfElementDisplayed = function() { $$.resizeIfElementDisplayed = function() {
// if element not displayed skip it // if element not displayed skip it
if (!$$.api.element.offsetParent) { if ($$.api == null || !$$.api.element.offsetParent) {
return; return;
} }
@ -948,9 +948,9 @@ c3_chart_internal_fn.bindResize = function () {
}; };
if (window.attachEvent) { if (window.attachEvent) {
window.attachEvent('onresize', resizeIfElementDisplayed); window.attachEvent('onresize', $$.resizeIfElementDisplayed);
} else if (window.addEventListener) { } else if (window.addEventListener) {
window.addEventListener('resize', resizeIfElementDisplayed, false); window.addEventListener('resize', $$.resizeIfElementDisplayed, false);
} else { } else {
// fallback to this, if this is a very old browser // fallback to this, if this is a very old browser
var wrapper = window.onresize; var wrapper = window.onresize;

Loading…
Cancel
Save