calling bpy.ops.wm.open_mainfile from class which is invoked from pop-up button results in crash.
Closed, ArchivedPublic

Description

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

Details

Type
Bug
Julian Eisel (Severin) closed this task as "Archived".EditedOct 26 2016, 12:12 AM

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

When closing Blender or loading a new file, all open popups are closed and since rB4559229163, 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 rB4559229163 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 T49199, 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:

1diff --git a/source/blender/makesrna/intern/rna_wm_api.c b/source/blender/makesrna/intern/rna_wm_api.c
2index 8d0b704..b47eeff 100644
3--- a/source/blender/makesrna/intern/rna_wm_api.c
4+++ b/source/blender/makesrna/intern/rna_wm_api.c
5@@ -164,6 +164,15 @@ static int rna_Operator_props_popup(bContext *C, wmOperator *op, wmEvent *event)
6​ return WM_operator_props_popup(C, op, event);
7​ }
8
9+static void rna_Operator_popup_confirm(bContext *C)
10+{
11+ ARegion *menu = CTX_wm_menu(C);
12+
13+ if (menu) {
14+ UI_popup_menu_retval_set(menu->uiblocks.first, UI_RETURN_OK, true);
15+ }
16+}
17+
18​ static wmKeyMapItem *rna_KeyMap_item_new(wmKeyMap *km, ReportList *reports, const char *idname, int type, int value,
19​ int any, int shift, int ctrl, int alt, int oskey, int keymodifier, int head)
20​ {
21@@ -469,6 +478,8 @@ void RNA_api_wm(StructRNA *srna)
22​ "(only to let user confirm the execution, no operator properties shown)");
23​ rna_generic_op_invoke(func, WM_GEN_INVOKE_EVENT | WM_GEN_INVOKE_RETURN);
24
25+ func = RNA_def_function(srna, "popup_return_ok", "rna_Operator_popup_confirm");
26+ RNA_def_function_flag(func, FUNC_NO_SELF | FUNC_USE_CONTEXT);
27
28​ /* wrap UI_popup_menu_begin */
29​ 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:

. 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.

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