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

Memory leaks fixed #515

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dev/src/ScrollMagic/Scene/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ this.remove = function () {
this.destroy = function (reset) {
Scene.trigger("destroy", {reset: reset});
Scene.remove();
//need to clear triggerElement reference avoid memory leaks and detached dom nodes
Scene.triggerElement(null);
Scene.off("*.*");
log(3, "destroyed " + NAMESPACE + " (reset: " + (reset ? "true" : "false") + ")");
return null;
Expand Down
2 changes: 2 additions & 0 deletions dev/src/ScrollMagic/Scene/feature-pinning.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ this.removePin = function (reset) {
_pin.removeEventListener("mousewheel", onMousewheelOverPin);
_pin.removeEventListener("DOMMouseScroll", onMousewheelOverPin);
_pin = undefined;
//remove reference to spacer elements to avoid memory leaks and avoid detached dom nodes
_pinOptions.spacer = null;
log(3, "removed pin (reset: " + (reset ? "true" : "false") + ")");
}
return Scene;
Expand Down
62 changes: 39 additions & 23 deletions scrollmagic/uncompressed/ScrollMagic.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@
_options.container.addEventListener("resize", onChange);
_options.container.addEventListener("scroll", onChange);

_options.refreshInterval = parseInt(_options.refreshInterval) || DEFAULT_OPTIONS.refreshInterval;
var ri = parseInt(_options.refreshInterval, 10);
_options.refreshInterval = _util.type.Number(ri) ? ri : DEFAULT_OPTIONS.refreshInterval;
scheduleRefresh();

log(3, "added new " + NAMESPACE + " controller (v" + ScrollMagic.version + ")");
Expand Down Expand Up @@ -1316,6 +1317,8 @@
reset: reset
});
Scene.remove();
//need to clear triggerElement reference avoid memory leaks and detached dom nodes
Scene.triggerElement(null);
Scene.off("*.*");
log(3, "destroyed " + NAMESPACE + " (reset: " + (reset ? "true" : "false") + ")");
return null;
Expand Down Expand Up @@ -1578,31 +1581,40 @@
var
elementPos = 0,
telem = _options.triggerElement;
if (_controller && telem) {
var
controllerInfo = _controller.info(),
containerOffset = _util.get.offset(controllerInfo.container),
// container position is needed because element offset is returned in relation to document, not in relation to container.
param = controllerInfo.vertical ? "top" : "left"; // which param is of interest ?
// if parent is spacer, use spacer position instead so correct start position is returned for pinned elements.
while (telem.parentNode.hasAttribute(PIN_SPACER_ATTRIBUTE)) {
telem = telem.parentNode;
}
if (_controller && (telem || _triggerPos > 0)) { // either an element exists or was removed and the triggerPos is still > 0
if (telem) { // there currently a triggerElement set
if (telem.parentNode) { // check if element is still attached to DOM
var
controllerInfo = _controller.info(),
containerOffset = _util.get.offset(controllerInfo.container),
// container position is needed because element offset is returned in relation to document, not in relation to container.
param = controllerInfo.vertical ? "top" : "left"; // which param is of interest ?
// if parent is spacer, use spacer position instead so correct start position is returned for pinned elements.
while (telem.parentNode.hasAttribute(PIN_SPACER_ATTRIBUTE)) {
telem = telem.parentNode;
}

var elementOffset = _util.get.offset(telem);
var elementOffset = _util.get.offset(telem);

if (!controllerInfo.isDocument) { // container is not the document root, so substract scroll Position to get correct trigger element position relative to scrollcontent
containerOffset[param] -= _controller.scrollPos();
if (!controllerInfo.isDocument) { // container is not the document root, so substract scroll Position to get correct trigger element position relative to scrollcontent
containerOffset[param] -= _controller.scrollPos();
}

elementPos = elementOffset[param] - containerOffset[param];

} else { // there was an element, but it was removed from DOM
log(2, "WARNING: triggerElement was removed from DOM and will be reset to", undefined);
Scene.triggerElement(undefined); // unset, so a change event is triggered
}
}

elementPos = elementOffset[param] - containerOffset[param];
}
var changed = elementPos != _triggerPos;
_triggerPos = elementPos;
if (changed && !suppressEvents) {
Scene.trigger("shift", {
reason: "triggerElementPosition"
});
var changed = elementPos != _triggerPos;
_triggerPos = elementPos;
if (changed && !suppressEvents) {
Scene.trigger("shift", {
reason: "triggerElementPosition"
});
}
}
};

Expand All @@ -1618,6 +1630,7 @@
}
};


var _validate = _util.extend(SCENE_OPTIONS.validate, {
// validation for duration handled internally for reference to private var _durationMethod
duration: function (val) {
Expand Down Expand Up @@ -2298,6 +2311,8 @@
_pin.removeEventListener("mousewheel", onMousewheelOverPin);
_pin.removeEventListener("DOMMouseScroll", onMousewheelOverPin);
_pin = undefined;
//remove reference to spacer elements to avoid memory leaks and avoid detached dom nodes
_pinOptions.spacer = null;
log(3, "removed pin (reset: " + (reset ? "true" : "false") + ")");
}
return Scene;
Expand Down Expand Up @@ -2400,7 +2415,7 @@
val = val || undefined;
if (val) {
var elem = _util.get.elements(val)[0];
if (elem) {
if (elem && elem.parentNode) {
val = elem;
} else {
throw ["Element defined in option \"triggerElement\" was not found:", val];
Expand Down Expand Up @@ -2468,6 +2483,7 @@
};



/**
* TODO: DOCS (private for dev)
* @class
Expand Down