MinaPecheux/godot-tutorials

Possible FSM Memory Leak

Closed this issue · 2 comments

Hi Mina, thanks for your C# tutorial on finite state machines (https://www.youtube.com/watch?v=Kcg1SEgDqyk). I'm using a similar FSM pattern in my own project, and I wanted to ask whether you have observed a potential memory leak via the object count when using a very basic FSM setup. When I create an empty idle state, I notice the object count in the debugger > monitors slowly climbing. Eventually, garbage collection occurs, but this can be after tens of thousands of objects have been created.

Here's a memory profile from your FSM demo

Screenshot from 2023-09-27 10-43-53

Here it is in my project:

memory_leak

Any idea what could be causing this?

Steps to reproduce:

  1. Git clone your godot-tutorials repository
  2. Run the FSM demo.tscn

Environment:

Godot v4.1.0
Dotnet SDK : 7.0.111

Oh I think I managed to solve this!

Screenshot from 2023-09-27 16-52-30

According to this forum post, we have to call @event.Dispose(); or using(@event){ ... } within the input handler of the FSM:

    public override void _UnhandledInput(InputEvent @event)
    {
        _currentState.HandleInput(@event);
        @event.Dispose();
    }

Seems like a known memory leak: godotengine/godot#41543

Hello,
and wow - many thanks for finding the bug AND solving it!! So cool :)

I'm indeed quite new to using the Input functions in Godot, so I definitely missed that - and didn't think to check memory leaks on a simple project like this one ^^

This is extremely cool - it gives me some ideas+extra material for an upcoming tutorials on handling inputs in Godot and monitoring your games.

Many thanks again - I've fixed the code according to your recommendation!