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

SceneTree.CreateTimer() does not free the timer after its time elapses #71081

Closed
Mooncat25 opened this issue Jan 8, 2023 · 1 comment · Fixed by #87526
Closed

SceneTree.CreateTimer() does not free the timer after its time elapses #71081

Mooncat25 opened this issue Jan 8, 2023 · 1 comment · Fixed by #87526

Comments

@Mooncat25
Copy link

Godot version

4.0.beta10.mono.official [d0398f6]

System information

Windows 11 Home 64-bit (Build 22621.963), Intel Core i5 11400F, NVIDIA GeForce RTX 3070

Issue description

While the document says "The timer will be automatically freed after its time elapses.", the Debugger says the opposite.

The timer created by SceneTree.CreateTimer() is not freed automatically after its time elapses. The Object count and Memory usage in the Debugger tab keep increasing.

Steps to reproduce

  1. Create new project.
  2. Create C# script CreateTimerTester.cs with the following contents:
using Godot;
public partial class CreateTimerTester : Node2D
{
    public override void _Ready()
    {
        base._Ready();
        TestCreateTimer();
    }

    private async void TestCreateTimer()
    {
        var tree = GetTree();
        do
        {
            await ToSignal(tree.CreateTimer(0.1, true, false, true), "timeout");
        }
        while (true);
    }
}
  1. Create new scene. Create a Node2D. Attach the script to the node.
  2. Play the scene. Observe the Object count and Memory usage in the Debugger tab.

(Optional) 5. If you call TestCreateTimer(); multiple times with a for-loop, it can be more effective. Here is a screenshot of calling it 5000 times and then stopping the loops by a cancellation token after 10 seconds:
Screenshot 2023-01-08 174024
The full code for step 5 is in the reproduction project.

Minimal reproduction project

TestCreateTimer.zip

@Mooncat25
Copy link
Author

To make sure the issue is not caused by Object.ToSignal(), I have also changed the script as follow and tested again:

using Godot;
public partial class CreateTimerTester : Node2D
{
    public override void _Ready()
    {
        base._Ready();
        TestCreateTimer();
    }

    private void TestCreateTimer() =>
        GetTree().CreateTimer(0.1, true, false, true).Timeout += OnTimeOut;

    private void OnTimeOut() => TestCreateTimer();
}

Result is the same.

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