Memory leak related to the gpu module #119028

Open
opened 2024-03-03 14:58:49 +01:00 by Damien Picard · 5 comments
Member

System Information
Operating system: Linux-6.5.0-21-generic-x86_64-with-glibc2.38 64 Bits, X11 UI
Graphics card: NVIDIA GeForce RTX 3060 Laptop GPU/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 545.29.06

Blender Version
Broken: version: 4.2.0 Alpha, branch: main, commit date: 2024-03-02 23:40, hash: 62bb346af911
Broken: version: 3.3.15, branch: master, commit date: 2024-01-15 13:02, hash: ec91541af1fa
Worked: version: 3.2.0

Short description of error
Under specific circumstances probably related to the gpu module and property updates, a memory leak occurs.

Exact steps for others to reproduce the error

  • Start Blender with the --debug-memory option.
  • Install the attached add-on stub.
  • Enable it in the User Preferences.
  • Quit Blender
  • This message is displayed in the terminal:
Error: Not freed memory blocks: 4, total unfreed memory 0.001808 MB
GLShader len: 1648 0x7f966df95c40
GLShaderInterface len: 248 0x7f965badc040
GLShaderInterface len: 0 0x7f967f5cfa78
name_buffer len: 0 0x7f967f5cfab8

Without --debug-memory, only the first line appears.
The error does not appear when the script is run in a text editor.
This was originally reported in blender/blender-addons#105201 with the Sun Position add-on.

Full script:

bl_info = {
    "name": "Memory Leak Debug",
    "version": (0, 0, 1),
    "blender": (2, 80, 0),
    "category": "Debug",
}

import bpy
import gpu

shader_info = gpu.types.GPUShaderCreateInfo()
shader_info.vertex_source('void main() {}')
shader_info.fragment_source('void main() {}')

shader = gpu.shader.create_from_info(shader_info)
del shader_info

def test_update(self, context):
    pass

class TestProperties(bpy.types.PropertyGroup):
    test: bpy.props.BoolProperty(name="Test", update=test_update)

def register():
    bpy.utils.register_class(TestProperties)

def unregister():
    bpy.utils.unregister_class(TestProperties)
**System Information** Operating system: Linux-6.5.0-21-generic-x86_64-with-glibc2.38 64 Bits, X11 UI Graphics card: NVIDIA GeForce RTX 3060 Laptop GPU/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 545.29.06 **Blender Version** Broken: version: 4.2.0 Alpha, branch: main, commit date: 2024-03-02 23:40, hash: `62bb346af911` Broken: version: 3.3.15, branch: master, commit date: 2024-01-15 13:02, hash: `ec91541af1fa` Worked: version: 3.2.0 **Short description of error** Under specific circumstances probably related to the `gpu` module and property updates, a memory leak occurs. **Exact steps for others to reproduce the error** - Start Blender with the `--debug-memory` option. - Install the attached add-on stub. - Enable it in the User Preferences. - Quit Blender - This message is displayed in the terminal: ``` Error: Not freed memory blocks: 4, total unfreed memory 0.001808 MB GLShader len: 1648 0x7f966df95c40 GLShaderInterface len: 248 0x7f965badc040 GLShaderInterface len: 0 0x7f967f5cfa78 name_buffer len: 0 0x7f967f5cfab8 ``` Without `--debug-memory`, only the first line appears. The error does not appear when the script is run in a text editor. This was originally reported in blender/blender-addons#105201 with the Sun Position add-on. Full script: ```python bl_info = { "name": "Memory Leak Debug", "version": (0, 0, 1), "blender": (2, 80, 0), "category": "Debug", } import bpy import gpu shader_info = gpu.types.GPUShaderCreateInfo() shader_info.vertex_source('void main() {}') shader_info.fragment_source('void main() {}') shader = gpu.shader.create_from_info(shader_info) del shader_info def test_update(self, context): pass class TestProperties(bpy.types.PropertyGroup): test: bpy.props.BoolProperty(name="Test", update=test_update) def register(): bpy.utils.register_class(TestProperties) def unregister(): bpy.utils.unregister_class(TestProperties) ```
Damien Picard added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2024-03-03 14:58:50 +01:00
Member

Thanks for the report. Can confirm.
cc @Jeroen-Bakker

Thanks for the report. Can confirm. cc @Jeroen-Bakker
Pratik Borhade added
Module
Python API
Status
Confirmed
Interest
EEVEE & Viewport
and removed
Status
Needs Triage
labels 2024-03-04 05:12:20 +01:00
Member

In the unregister any resources should be cleaned up similar to when using BMesh. In this case this isn't done. So this seems to be expected. The python GPUShader types has a destructor that cleans up the resources, but global resources held by python are kept until blender exits.

The lifetime of resources linked to python objects should be handled by the add-on. In the given example this isn't done.
It can be fixed by using

def unregister():
    bpy.utils.unregister_class(TestProperties)
    del shader

Unsure this classifies as a bug. Would like to have feedback from the python module on this topic.

In the unregister any resources should be cleaned up similar to when using BMesh. In this case this isn't done. So this seems to be expected. The python GPUShader types has a destructor that cleans up the resources, but global resources held by python are kept until blender exits. The lifetime of resources linked to python objects should be handled by the add-on. In the given example this isn't done. It can be fixed by using ```python def unregister(): bpy.utils.unregister_class(TestProperties) del shader ``` Unsure this classifies as a bug. Would like to have feedback from the python module on this topic.
Jeroen Bakker added
Status
Needs Info from Developers
and removed
Status
Confirmed
labels 2024-03-04 08:20:49 +01:00
Author
Member

Interesting, I didn’t know that since the gpu examples in the docs don’t illustrate using register().

The thing that looks quite fishy to me is that the issue occurs only when there is an update in a property inside a property group. So, replacing:

    test: bpy.props.BoolProperty(name="Test", update=test_update)

with:

    test: bpy.props.BoolProperty(name="Test")

fixes the issue. And this does not seem related to the gpu at all, since this update function is empty!

Interesting, I didn’t know that since the `gpu` examples in the docs don’t illustrate using `register()`. The thing that looks quite fishy to me is that the issue occurs only when there is an update in a property inside a property group. So, replacing: ```python test: bpy.props.BoolProperty(name="Test", update=test_update) ``` with: ```python test: bpy.props.BoolProperty(name="Test") ``` fixes the issue. And this does not seem related to the gpu at all, since this update function is empty!
Member

Yes I came to the same conclusion that the GPU examples are just scripts and don't show an example with a full add-on. The templates only use built-in shaders, which don't have this issue.

Removing the update callback is also something strange. Perhaps Blender reevaluates the global parts of the script which creates a second shader and overwrites the previous python handler.
Can you check if you safe guard the initialization of the shader would also fix the issue?

Yes I came to the same conclusion that the GPU examples are just scripts and don't show an example with a full add-on. The templates only use built-in shaders, which don't have this issue. Removing the update callback is also something strange. Perhaps Blender reevaluates the global parts of the script which creates a second shader and overwrites the previous python handler. Can you check if you safe guard the initialization of the shader would also fix the issue?
Author
Member

Can you check if you safe guard the initialization of the shader would also fix the issue?

I’m not sure how to do that in Python, because the variable name doesn’t appear in globals() even if the script is run multiple times. A print next to the shader init displays only once though, so I don’t think that’s happening.

> Can you check if you safe guard the initialization of the shader would also fix the issue? I’m not sure how to do that in Python, because the variable name doesn’t appear in `globals()` even if the script is run multiple times. A `print` next to the shader init displays only once though, so I don’t think that’s happening.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#119028
No description provided.