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

RenderingServer.create_local_rendering_device() memory leaks #71003

Open
VanessB opened this issue Jan 6, 2023 · 3 comments
Open

RenderingServer.create_local_rendering_device() memory leaks #71003

VanessB opened this issue Jan 6, 2023 · 3 comments

Comments

@VanessB
Copy link

VanessB commented Jan 6, 2023

Godot version

v4.0.beta10.official

System information

Debian GNU/Linux 11 (bullseye), Intel Core i3-8130U CPU, Mesa Intel UHD Graphics 620 (KBL GT2)

Issue description

Memory leaks occur while using RenderingServer.create_local_rendering_device(). Moreover, the engine crashes after creating many local rendering devices (even if this is done in a scoped environment, see below). If rendering_device.free() is called, the following warning appears: drivers/vulkan/rendering_device_vulkan.cpp:8957 - 1 RID of type "<...>" was leaked., where <...> is any object, created with local rendering device (Shader, Texture, ...).

Steps to reproduce

The following code reproduces the bug:

extends Node

func _ready() -> void:
    for step in range(199):  # 199 times are enogh to crash the engine on my system.
        create_lrd()
        print(step)

func create_lrd() -> void:
    var rd := RenderingServer.create_local_rendering_device()

    # If something is done with rd here, it remains in memory even upon
    # return from this function.

    #rd.free() # No memory leaks occur, but Vulkan warnings are printed.
    # this line also fixes crashes.

Minimal reproduction project

RDMemoryLeak.zip

@clayjohn clayjohn added this to the 4.x milestone Jan 6, 2023
@VanessB VanessB changed the title RenderingServer.create_local_rendering_device() memory leaks RenderingServer.create_local_rendering_device() memory leaks Jan 13, 2023
@wardensdev
Copy link

wardensdev commented Jan 16, 2023

Creating that many local rendering devices will use up all of your video memory/gpu resources (per the vulkan spec), resulting in a crash. The limit I can go on my machine is ~100, the amount varies on different devices. You have to call RenderingDevice.free_rid(RID) for any resources created with the rendering device manually, else they will be leaked when the RenderingDevice is freed.

I've written a small function in one of my projects to get the total memory usage for all created rendering devices to prevent too many RDs from causing a crash (Edit: it seems it isn't necessarily related to memory, but different devices do have a limit on how many logical devices can be created at one time). Maybe having this functionality built in would solve some headaches for people trying to use RenderingDevice before it gets documented. Automatically freeing RD-created resources might be a good thing as well, but I don't know enough about the implementation to say whether that's feasible.

Edit: My mind wandered a bit off track here. I do see the issue of them staying in memory when the function returns due to them not being reference counted.

@Chaosus
Copy link
Member

Chaosus commented Jan 16, 2023

Calling RenderingServer.free_rid(rd) to free the local device will help maybe?

@VanessB
Copy link
Author

VanessB commented Jan 16, 2023

Calling RenderingServer.free_rid(rd) to free the local device will help maybe?

Yes, it helps, but it is still confusing that it is not done automatically. At least this should be reflected in the docs.

EDIT: did not read your suggestion carefully. RenderingServer.free_rid(rd) actually has no effect. You need to rd.free_rid(resource_id) manually for every resource created with given rendering device.

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

No branches or pull requests

4 participants