Crash after freeing up bmesh

Hey, I’m getting some blender-crashes when working with bmeshes. Running this code twice will cause blender to crash (2.92).

import bpy
import bmesh

obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)

# do amazing stuff to my bmesh here

bmesh.update_edit_mesh(obj.data)
bm.free()

Output when running blender from console:

>     Traceback (most recent call last):
>       File "\Text", line 13, in <module>
>     Error   : EXCEPTION_ACCESS_VIOLATION
>     Address : 0x00007FFA04F89FFD
>     Module  : python37.dll
>     Thread  : 00000d7c
>     Writing: C:\Users\Mortenb\AppData\Local\Temp\blender.crash.txt

I’m pretty new to this software, so i might be using the bmesh wrong, but from what I could see in the documentation I should use free() after i’m done with it?

Is this a bug, or just me using it all wrong? Should I just omit the bm.free(), or will this cause other problems?

I’ve attached the crash-log if anyone feels like diving deeper into this :slight_smile:

TLDR: don’t use free() in edit mode.

When you’re in edit mode, a bmesh object already exists and is shared by all edit mode operators that need a bmesh. when you call from_edit_mesh you’re just getting a reference to it. When you manually free it, you’ve removed the reference that every other edit mode operation relies on, and blender crashes.

2 Likes