calling bpy.ops.wm.open_mainfile from class which is invoked from pop-up button results in crash. #49828

Closed
opened 2016-10-24 16:44:09 +02:00 by Mark aka Dark · 7 comments
Member

Notice
I'm not sure if this is a bug. But it looks like it, because before Blender 2.78 this crash didn't occure.

System Information
OS's: Arch linux, windows 10
GPU's: nvidia gtx950m and several others.

Blender Version
Broken: 2.78 4bb1e22
Worked: 2.77a abf6f08 (is from arch linux repo i think)
The code is part of the blenchmark add-on. Several users reported that it is not working since Blender version 2.78

Short description of error
calling bpy.ops.wm.open_mainfile from class which is invoked from pop-up button results in crash.

Exact steps for others to reproduce the error

  1. install and enable attached addon (samplescriptje)
  2. click Render > sample
  3. click ok in popup
  4. crash

attachment
samplescriptje.zip

**Notice** I'm not sure if this is a bug. But it looks like it, because before Blender 2.78 this crash didn't occure. **System Information** OS's: Arch linux, windows 10 GPU's: nvidia gtx950m and several others. **Blender Version** Broken: 2.78 4bb1e22 Worked: 2.77a abf6f08 (is from arch linux repo i think) The code is part of the blenchmark add-on. Several users reported that it is not working since Blender version 2.78 **Short description of error** calling bpy.ops.wm.open_mainfile from class which is invoked from pop-up button results in crash. **Exact steps for others to reproduce the error** 1. install and enable attached addon (samplescriptje) 2. click Render > sample 3. click ok in popup 4. crash **attachment** [samplescriptje.zip](https://archive.blender.org/developer/F385809/samplescriptje.zip)
Author
Member

Changed status to: 'Open'

Changed status to: 'Open'
Author
Member

Added subscriber: @mark_aka_dark

Added subscriber: @mark_aka_dark

Added subscribers: @JulianEisel, @Sergey

Added subscribers: @JulianEisel, @Sergey
Julian Eisel was assigned by Sergey Sharybin 2016-10-25 10:58:37 +02:00

@JulianEisel, another one caused by 4559229.

@JulianEisel, another one caused by 4559229.
Member

Changed status from 'Open' to: 'Archived'

Changed status from 'Open' to: 'Archived'
Member

Hmmm, after quite some investigation I conclude this is wrong usage of our popup code. It's a similar issue as #49199, but much trickier.

When closing Blender or loading a new file, all open popups are closed and since 4559229163, all data of these popups is freed then to fix memory leaks. Note that your script had these memory leaks too (open a Blender version that doesn't have 4559229163 applied from a terminal, run your script and close Blender - it should print memory-leak errors). These memory leaks are fixed now, however, combined with wm.open_mainfile it causes use-after-free errors in delayed function execution... an old & ugly hack of our interface interaction code.

For #49199, it was possible to create an exception to handle data freeing properly. The difference to this case is that the confirmation button is created from .py, from which we can't register a button execution callback that sets the exception and closes the popup properly.
We could add some way to mark a button as popup confirmation button, but this would be really ugly (e.g. we had to add a is_popup_confirm boolean to uiLayout.operator).
Another (ugly) way to fix this crash would be this:
P421: (An Untitled Masterwork)

diff --git a/source/blender/makesrna/intern/rna_wm_api.c b/source/blender/makesrna/intern/rna_wm_api.c
index 8d0b704..b47eeff 100644
--- a/source/blender/makesrna/intern/rna_wm_api.c
+++ b/source/blender/makesrna/intern/rna_wm_api.c
@@ -164,6 +164,15 @@ static int rna_Operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
        return WM_operator_props_popup(C, op, event);
 }
 
+static void rna_Operator_popup_confirm(bContext *C)
+{
+       ARegion *menu = CTX_wm_menu(C);
+
+       if (menu) {
+               UI_popup_menu_retval_set(menu->uiblocks.first, UI_RETURN_OK, true);
+       }
+}
+
 static wmKeyMapItem *rna_KeyMap_item_new(wmKeyMap *km, ReportList *reports, const char *idname, int type, int value,
                                          int any, int shift, int ctrl, int alt, int oskey, int keymodifier, int head)
 {
@@ -469,6 +478,8 @@ void RNA_api_wm(StructRNA *srna)
                                        "(only to let user confirm the execution, no operator properties shown)");
        rna_generic_op_invoke(func, WM_GEN_INVOKE_EVENT | WM_GEN_INVOKE_RETURN);
 
+       func = RNA_def_function(srna, "popup_return_ok", "rna_Operator_popup_confirm");
+       RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
 
        /* wrap UI_popup_menu_begin */
        func = RNA_def_function(srna, "pupmenu_begin__internal", "rna_PupMenuBegin");

You simply had to call wm.popup_return_ok()before object.benchmark_operator() in the script. It would bring back the memory leak though...


After all I'd say it's luck that your script worked fine previously (except of memory leaks). You create a popup that isn't closed correctly prior to calling wm.open_mainfile and I can't see a proper way to allow doing this. There are ways to create confirmation-popups from .py that work fine even when calling wm.open_mainfile, I strongly suggest to use them instead (e.g. check wm.invoke_props_dialog). I did some modifications to your init.py file show you how that would work: init.py. Downside is that there's no "Cancel" button and that you can't edit the button text though.

I'll consider this a limitation or hard-to-fix-corner-case for now. IMHO it's a bad idea to do biggish or ugly changes to interface and BPY to get this corner-case to work while there are pre-made confirmation popups available that behave properly even in this corner-case.
Closing the report, thanks for reporting though.

Hmmm, after quite some investigation I conclude this is wrong usage of our popup code. It's a similar issue as #49199, but much trickier. When closing Blender or loading a new file, all open popups are closed and since 4559229163, all data of these popups is freed then to fix memory leaks. Note that your script had these memory leaks too (open a Blender version that doesn't have 4559229163 applied from a terminal, run your script and close Blender - it should print memory-leak errors). These memory leaks are fixed now, however, combined with `wm.open_mainfile` it causes use-after-free errors in delayed function execution... an old & ugly hack of our interface interaction code. For #49199, it was possible to create an exception to handle data freeing properly. The difference to this case is that the confirmation button is created from .py, from which we can't register a button execution callback that sets the exception and closes the popup properly. We could add some way to mark a button as popup confirmation button, but this would be really ugly (e.g. we had to add a `is_popup_confirm` boolean to `uiLayout.operator`). Another (ugly) way to fix this crash would be this: [P421: (An Untitled Masterwork)](https://archive.blender.org/developer/P421.txt) ``` diff --git a/source/blender/makesrna/intern/rna_wm_api.c b/source/blender/makesrna/intern/rna_wm_api.c index 8d0b704..b47eeff 100644 --- a/source/blender/makesrna/intern/rna_wm_api.c +++ b/source/blender/makesrna/intern/rna_wm_api.c @@ -164,6 +164,15 @@ static int rna_Operator_props_popup(bContext *C, wmOperator *op, wmEvent *event) return WM_operator_props_popup(C, op, event); } +static void rna_Operator_popup_confirm(bContext *C) +{ + ARegion *menu = CTX_wm_menu(C); + + if (menu) { + UI_popup_menu_retval_set(menu->uiblocks.first, UI_RETURN_OK, true); + } +} + static wmKeyMapItem *rna_KeyMap_item_new(wmKeyMap *km, ReportList *reports, const char *idname, int type, int value, int any, int shift, int ctrl, int alt, int oskey, int keymodifier, int head) { @@ -469,6 +478,8 @@ void RNA_api_wm(StructRNA *srna) "(only to let user confirm the execution, no operator properties shown)"); rna_generic_op_invoke(func, WM_GEN_INVOKE_EVENT | WM_GEN_INVOKE_RETURN); + func = RNA_def_function(srna, "popup_return_ok", "rna_Operator_popup_confirm"); + RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT); /* wrap UI_popup_menu_begin */ func = RNA_def_function(srna, "pupmenu_begin__internal", "rna_PupMenuBegin"); ``` You simply had to call `wm.popup_return_ok()`before `object.benchmark_operator()` in the script. It would bring back the memory leak though... ---- After all I'd say it's luck that your script worked fine previously (except of memory leaks). You create a popup that isn't closed correctly prior to calling `wm.open_mainfile` and I can't see a proper way to allow doing this. There are ways to create confirmation-popups from .py that work fine even when calling `wm.open_mainfile`, I strongly suggest to use them instead (e.g. check `wm.invoke_props_dialog`). I did some modifications to your __init__.py file show you how that would work: [__init__.py](https://archive.blender.org/developer/F386373/__init__.py). Downside is that there's no "Cancel" button and that you can't edit the button text though. I'll consider this a limitation or hard-to-fix-corner-case for now. IMHO it's a bad idea to do biggish or ugly changes to interface and BPY to get this corner-case to work while there are pre-made confirmation popups available that behave properly even in this corner-case. Closing the report, thanks for reporting though.
Author
Member

Hi Julian,

Thank you for the extended explanation for this behaviour and your effort to show me a work-around. wm.invoke_props_dialog() is working fine, the downsides of it are not important. At least not for me.

Thanks again for your effort.
Mark

Hi Julian, Thank you for the extended explanation for this behaviour and your effort to show me a work-around. wm.invoke_props_dialog() is working fine, the downsides of it are not important. At least not for me. Thanks again for your effort. Mark
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#49828
No description provided.