Monday, April 28, 2025
HomeGame Developmentgodot - Is await/free in gdscript a possible reminiscence leak?

godot – Is await/free in gdscript a possible reminiscence leak?


I have no idea if it being a coroutine makes a distinction. However I can inform you that await has no technique of being cancelled, and sure it may be there ceaselessly and leak reminiscence.

Utilizing await creates a GDScriptFunctionState (this was uncovered in Godot 3, however it’s not in Godot 4). The GDScriptFunctionState internally holds a reference to a CallState which Godot makes use of to renew execution (it has the stack et.al.), and it may clear the CallState to keep away from these leaks, however at present doesn’t. There’s a repair being labored however it did not make it for Godot 4.4.

By the way in which, your state of affairs isn’t the more severe: if the facet awaiting is freed, and the opposite half indicators anticipate a runtime error when Godot tries to renew execution on an object that’s now not there.


After I first migrated away from await, I made a decision to refactor to indicators as an alternative. Godot will disconnect the sign connections robotically when both get together is freed, so it causes no issues. Simply to be clear, await on sign isn’t a sign connection.

You should utilize the CONNECT_ONE_SHOT to inform Godot to disconnect the sign robotically when it’s acquired (or disconnect it on the continuation, which is normally not a lot bother anyway). And, by the way in which, keep in mind that you may benefit from nameless “lambda” strategies.

Refactoring your await code to make use of continuations would possibly get messy relying on what you might be doing.

Refactoring a loop from await to indicators is just like refactoring a loop to recursion. First, in case you are utilizing await in the midst of a loop, you may need to do a partial unroll so await occurs at first of the iteration, afterwards it’s straightforward to make the physique of the loop right into a continuation. Then you will discover that it’s a good suggestion to have a way make a Callable to itself utilizing bind to curry the arguments for the subsequent run, and connecting to a sign.

Some points you would possibly run into:

  • When you bind a node and it turns into invalid, it should bind as a null and fail. As a substitute go it inside an array or dictionary, which can be handy in case you are passing a number of objects without delay.

  • Within the odd case you might want to join the identical methodology of the identical occasion to a sign a number of instances (which Godot doesn’t permit), you would possibly wish to make a category to carry the arguments and dispatch the decision, so you possibly can join totally different situations of the category to the identical sign.

Worse case state of affairs you’ll make a state machine: have a worth to point what to run subsequent, and dispatch with a match assertion. Though I ended up resolving the conditions that I bumped into the place this was helpful by totally different means.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments