Memory leak or not?
After making my accessors enumerable, I'm currently using Tween.js as follows, because it's easy, and haven't spent the time to investigate if I'm doing it without causing any leaks. The following is in a React component's componentDidMount lifecycle method:
async componentDidMount() {
const rocket = this.refs.rocket
await rocket.ready
const {position, absoluteSize} = rocket
const rocketToEarthTween = new Tween(position)
rocketToEarthTween.to({
x:0,
y:0,
}, 2000)
rocketToEarthTween.start(performance.now())
const positionTask = Motor.addRenderTask(time => {
rocketToEarthTween.update(time)
if (position.x === 0 && position.y === 0) {
//rocketToEarthTween.end()
Motor.removeRenderTask(positionTask)
}
})
}
I'm starting the tween, then Motor.addRenderTask hooks into the browser's paint cycle with requestAnimationFrame.
Is that fine? Or do I need to call rocketToEarthTween.end() to clean things up and prevent memory leaks?
You don't need to call tween.end or tween.remove unless you expect that the tween won't be updated with a timestamp greater than or equal to its ending time. I don't think you have that problem here. As long as removeRenderTask removes the reference to the anonymous lambda function, then I don't see any memory leaks here.
However, I would recommend that you put the removeRenderTask call in the tween's onCompleteCallback, so that you don't have to check the ending values explicitly.
On Aug 30, 2016 4:14 PM, "Joseph Orbegoso Pea" [email protected] wrote:
After making my accessors enumerable https://github.com/tweenjs/tween.js/issues/267, I'm currently using Tween.js as follows, because it's easy, and haven't spent the time to investigate if I'm doing it without causing any leaks. The following is in a React component's componentDidMount lifecycle method:
async componentDidMount() { const rocket = this.refs.rocket await rocket.ready const {position, absoluteSize} = rocket const rocketToEarthTween = new Tween(position) rocketToEarthTween.to({ x:0, y:0, }, 2000) rocketToEarthTween.start(performance.now()) console.log('start the tween!') const positionTask = Motor.addRenderTask(time => { console.log('update the tween!', position.x) rocketToEarthTween.update(time) if (position.x === 0 && position.y === 0) { //rocketToEarthTween.end() Motor.removeRenderTask(positionTask) } }) }I'm starting the tween, then Motor.addRenderTask hooks into the browser's paint cycle with requestAnimationFrame.
Is that fine? Or do I need to call rocketToEarthTween.end() to clean things up and prevent memory leaks?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/tweenjs/tween.js/issues/289, or mute the thread https://github.com/notifications/unsubscribe-auth/ACCav0BIseQQumgo4AAVpZk1oeLyUWxrks5qlLllgaJpZM4JxEvT .
hi, tweenjs is the best engine i have used but I notice that
In TWEEN.update (Group)

However in Tween's instance object, when we use update, the 'used tween animation' will not be deleted. So if i want the lib to help me deal with memory leak, i should use TWEEN.update or Group.update, and if i use Tween instance object's update, i should remove 'used' by myself?
Hello @mengxun1437 glad you like the lib! I'm not completely sure what you mean. Would you be able to provide sample code on jsfiddle or CodePen?
Hello @mengxun1437 glad you like the lib! I'm not completely sure what you mean. Would you be able to provide sample code on jsfiddle or CodePen?
I'm sorry I didn't express my question clearly and caused you trouble
I wrote an example on the codepen platform, here is the link: https://codepen.io/mengxun1437/pen/QWmmaJo?editors=1011
You can notice the console output,
Every time I will use a new Tween instance to achieve a piece of my animation (maybe this is not very elegant), if I use the 'update' of this instacnce in RAF to update, then the Tween instance on TWEEN (GROUP) There will be more and more, whether this will affect the performance of my application, I am not very clear about this, I hope to get your reply
Finally, I'm a newbie in 3D orientation, hope you can understand my description ability
Hi @mengxun1437
The codepen you wrote doesn't work for me. It doesn't call your update callback.
I recommend rewriting it without calling the tween instance's update function manually. Start by replacing line 33 like this:
//tween?.update()
TWEEN.update();
It's almost never a good idea to update a single tween manually.
Hi @mengxun1437
The codepen you wrote doesn't work for me. It doesn't call your update callback.
I recommend rewriting it without calling the tween instance's update function manually. Start by replacing line 33 like this:
//tween?.update() TWEEN.update();It's almost never a good idea to update a single tween manually.
i am very glad that you reply to me,and i think the way you recommend is the right way to use tween.js.
i just confused that why i can not use tween instance's update, i find it can achieve the the same goal.
the example in the codepen just an exapmle,so that's why it doesn't work for you, i write that example case just to ask why the tween instances are added to the global TWEEN._tweens and when they are completed, why they are not deleted. (different from using TWEEN.update)
if the TWEEN.update will delete the completed tween,why we use the tween instance's update, the completed tween are not deleted from the global TWEEN._tweens,i think these two cases's update are not in the same standard when the tween was completed. maybe we should delete it in tween instance's update,too? (just a suggestion)
anyway,very grateful for your reply!
It's almost never a good idea to update a single tween manually.
@mikebolt
I think it depends on use case, and in fact it is a very good idea.
The reason is because, when making components, as in differing parts of an app that may have their own separate animations, we want them to be in control of their own tweens, and not inadvertently updates in totally unrelated components.
Using TWEEN by multiple components is similar to using a shared global state by multiple components, and if they do not know what each are doing, it can lead to things not working.
I recommend using
- individual
Tweeninstances for single-tween animations TWEEN.Groupfor animations involving multiple tweens (f.e..chained tweens)- and
TWEENonly when the app is small and self-contained (like the whole app is a simple component by itself).
I believe we should update docs to use Groups and tweens, and avoid TWEEN, to lead people on a robust path whereby they will inherently avoid collision.
In a breaking change we can set the group parameter to default to false, possibly add a true option that restores adding to the default group, and otherwise require people to use tweens or Groups directly.
To be honest, I'd prefer to just have TWEEN be an API container, and not have it be a Group at all, and just have people use Groups or individual tweens.
But for backwards compatibility, it would be an easy upgrade path to have people pass TWEEN or true into the second arg. Maybe this can be a deprecated option for a while, then eventually we change the group type to just Group.
This is the only way to ensure robust multi-component development, while a shared global is not the ideal way to achieve that (without more complicated modifications to Group like perhaps adding labels for tweens, like a layering system, or something, but that is more complexity).
Note that currently, to avoid unintended interference of a component's tween by outside/unexpected manipulation of TWEEN, the tween should be instantiated with the second arg as false or a group instance:
const tween = new TWEEN.Tween(obj, false) // it will not be added to the TWEEN group
@mengxun1437 On the following line, tween is being set to undefined:
tween = generateTween()
because generateTween does not return anything.
Here's a working demo:
https://codepen.io/trusktr/pen/wvmjwwQ/536097270aeea3d13ccf286931369409?editors=0011
@trusktr Your reply helps me a lot, i get what you mean! Thanks!