Python/BMesh: Crash on UV changes when handling depsgraph update - CDLayer re-allocation issue #67093

Open
opened 2019-07-17 00:53:59 +02:00 by Benjamin Sauder · 13 comments

System Information
Operating system: Windows 10
Graphics card: GTX970

Blender Version
Broken: Blender 2.8 RC1

Short description of error
On an addon I subscribe to the depsgraph update handler. The handler grabs some data via bmesh - its not changing anything.
Testing with the default cube - everything works fine, one can move uvs around without issues.
But on a cube with 6 subdivision (24k verts) - moving uvs results often in corruption and soon blender crashes.
Interesstingly doing regular vert transformation in the 3d view doens't create any issues as far as I can tell.

Exact steps for others to reproduce the error

depsgraph_update_corrupt_uvs.blend

  1. load attached blend file
  2. run script in text editor (review script - its really simple)
  3. enter edit mode on the cube
  4. make a uv selection
  5. try moving uvs around
**System Information** Operating system: Windows 10 Graphics card: GTX970 **Blender Version** Broken: Blender 2.8 RC1 **Short description of error** On an addon I subscribe to the depsgraph update handler. The handler grabs some data via bmesh - its not changing anything. Testing with the default cube - everything works fine, one can move uvs around without issues. But on a cube with 6 subdivision (24k verts) - moving uvs results often in corruption and soon blender crashes. Interesstingly doing regular vert transformation in the 3d view doens't create any issues as far as I can tell. **Exact steps for others to reproduce the error** [depsgraph_update_corrupt_uvs.blend](https://archive.blender.org/developer/F7613796/depsgraph_update_corrupt_uvs.blend) 1. load attached blend file 2. run script in text editor (review script - its really simple) 3. enter edit mode on the cube 4. make a uv selection 5. try moving uvs around

Added subscriber: @kioku

Added subscriber: @kioku

#92620 was marked as duplicate of this issue

#92620 was marked as duplicate of this issue

Added subscribers: @Sergey, @ideasman42, @dfelinto

Added subscribers: @Sergey, @ideasman42, @dfelinto
Dalai Felinto self-assigned this 2019-07-17 01:50:35 +02:00

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'

I get the crash even with a simple cube (and debug + ASAN build) and the following script (simplified from the reported one):

import bpy
import bmesh

def depsgraph_handler(dummy):
    
    depsgraph = bpy.context.evaluated_depsgraph_get()
    for update in depsgraph.updates:
        if not hasattr(update.id, "type") or update.id.type != 'MESH':
            continue
        
        mesh = update.id.data
        bm = bmesh.from_edit_mesh(mesh)        
        #bm.free()

bpy.app.handlers.depsgraph_update_post.append(depsgraph_handler)

SUMMARY: AddressSanitizer: heap-use-after-free //source/blender/editors/transform/transform_conversions.c:3669 in flushTransUVs
Fullbacktrace: P1039

That said you are not freeing bm in your script. If you do that the crash goes away.

Closing for now, but poking other devs in case I'm missing something here:
cc @Sergey @ideasman42

I get the crash even with a simple cube (and debug + ASAN build) and the following script (simplified from the reported one): ``` import bpy import bmesh def depsgraph_handler(dummy): depsgraph = bpy.context.evaluated_depsgraph_get() for update in depsgraph.updates: if not hasattr(update.id, "type") or update.id.type != 'MESH': continue mesh = update.id.data bm = bmesh.from_edit_mesh(mesh) #bm.free() bpy.app.handlers.depsgraph_update_post.append(depsgraph_handler) ``` `SUMMARY: AddressSanitizer: heap-use-after-free //source/blender/editors/transform/transform_conversions.c:3669 in flushTransUVs` Fullbacktrace: [P1039](https://archive.blender.org/developer/P1039.txt) That said you are not freeing `bm` in your script. If you do that the crash goes away. Closing for now, but poking other devs in case I'm missing something here: cc @Sergey @ideasman42

@dfelinto thank you for looking into this.

I forgot to try bm.free() in this case but I was under the impression that one should not call this for a bm bm.from_edit_mesh. I remember me having issues calling bm.free and finding this:

https://developer.blender.org/T39121

@dfelinto thank you for looking into this. I forgot to try bm.free() in this case but I was under the impression that one should not call this for a bm bm.from_edit_mesh. I remember me having issues calling bm.free and finding this: https://developer.blender.org/T39121

Changed status from 'Archived' to: 'Open'

Changed status from 'Archived' to: 'Open'

Added subscriber: @mont29

Added subscriber: @mont29

@dfelinto, i don't think calling bm.free() is correct here. It doesn't make sense to free an internal data, and it only causes confusion in the script later on with AttributeError: 'bytes' object has no attribute 'faces'.

It seems that simply iterating over geometry confuses something with modal operator: possible re-allocations or tagging?

This isn't my area, leaving up to @ideasman42 and @mont29. I can reproduce the issue in 2.79, so doubt it's caused by the dependency graph or copy-on-write.

T67093_279.blend

@dfelinto, i don't think calling `bm.free()` is correct here. It doesn't make sense to free an internal data, and it only causes confusion in the script later on with `AttributeError: 'bytes' object has no attribute 'faces'`. It seems that simply iterating over geometry confuses something with modal operator: possible re-allocations or tagging? This isn't my area, leaving up to @ideasman42 and @mont29. I can reproduce the issue in 2.79, so doubt it's caused by the dependency graph or copy-on-write. [T67093_279.blend](https://archive.blender.org/developer/F7614410/T67093_279.blend)
Dalai Felinto was unassigned by Bastien Montagne 2019-07-22 15:18:35 +02:00
Campbell Barton was assigned by Bastien Montagne 2019-07-22 15:18:35 +02:00

Yes, bpy BM access seems to re-allocate some customdata memory, and since transform operator for UVs keeps a direct pointer to that UV CDLayer… it can do nothing but crash!

@ideasman42 I would rather leave this to you, think you have a much more complete view/understanding of those areas of code? I really do not see how to handle this case, besides not storing any pointer reference to CDLayer data from BMesh, in the modal operators.

Or maybe we should always ensure edit bmesh does have the CD_BM_ELEM_PYPTR? But that would only fix that case, any code adding some new layer will generate the same problem…

Yes, bpy BM access seems to re-allocate some customdata memory, and since transform operator for UVs keeps a direct pointer to that UV CDLayer… it can do nothing but crash! @ideasman42 I would rather leave this to you, think you have a much more complete view/understanding of those areas of code? I really do not see how to handle this case, besides not storing *any* pointer reference to CDLayer data from BMesh, in the modal operators. Or maybe we should always ensure edit bmesh does have the `CD_BM_ELEM_PYPTR`? But that would only fix that case, any code adding some new layer will generate the same problem…
Bastien Montagne changed title from Python: Crash on UV changes when handling depsgraph update to Python/BMesh: Crash on UV changes when handling depsgraph update - CDLayer re-allocation issue 2019-07-22 15:19:15 +02:00
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

#55954 (Python/BMesh: Add/removing textures between renders causes memory leak/ram crash) also seems to be related...

#55954 (Python/BMesh: Add/removing textures between renders causes memory leak/ram crash) also seems to be related...
Campbell Barton was unassigned by Dalai Felinto 2019-12-23 16:33:42 +01:00

Added subscribers: @randum, @mano-wii

Added subscribers: @randum, @mano-wii
Philipp Oeser removed the
Interest
Python API
label 2023-02-10 09:04:45 +01:00
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
7 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#67093
No description provided.