Page MenuHome

Call "bpy.ops.screen.screen_full_area()" 2 times causes EXCEPTION_ACCESS_VIOLATION
Closed, ArchivedPublic

Description

System Information
Operating system: Windows-10-10.0.19041-SP0 64 Bits
Graphics card: GeForce RTX 2060/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 432.00

Blender Version
Broken: version: 2.83.0, branch: master, commit date: 2020-06-03 14:38, hash: rB211b6c29f771
Worked: None

Short description of error
In text editor, the below code work fines :-

override = {'window': window, 'screen': screen, 'area': area}
bpy.ops.screen.screen_full_area(override)
#bpy.ops.screen.screen_full_area(override)

However, if I uncomment the line, Blender will crash.

Exact steps for others to reproduce the error
Try to run this script :-

import os.path
import bpy

print("=========BEGIN ====")
str_contextName='VIEW_3D'
bool_found = False
for window in bpy.context.window_manager.windows:
    screen = window.screen
    for area in screen.areas:
        if area.type == str_contextName:
            print(" ==> found context ")
            bpy.context.window.scene = bpy.context.scene
            override = {'window': window, 'screen': screen, 'area': area}
            bpy.ops.screen.screen_full_area(override)
            bpy.ops.screen.screen_full_area(override) #: try to uncomment = crash
            #^ will get "exception_access_violation" when exit this XXX.py 
            bool_found=True
            break
    
print("=========END====")

Event Timeline

hyyou (hyyou) updated the task description. (Show Details)
hyyou (hyyou) updated the task description. (Show Details)
hyyou (hyyou) updated the task description. (Show Details)
Germano Cavalcante (mano-wii) changed the task status from Needs Triage to Confirmed.Jul 16 2020, 2:56 PM
Germano Cavalcante (mano-wii) changed the subtype of this task from "Report" to "Bug".

This is related to the same problem described in T77419
Solving that problem will probably solve this one too.

But even without working as it should, the crash should be replaced with an error message.
Just as it should be in T54468

Looked into a fix, although the root of the error is that the area & screen being passed on the second call are invalid (freed memory).

A more general fix for this would be to validate all context members passed as overrides to operators from Python, to properly raise errors, as doing this from the operator would end up causing checks all over.

Once valid data is passed to the operator, this patch makes the functionality possible.

diff --git a/source/blender/editors/screen/screen_edit.c b/source/blender/editors/screen/screen_edit.c
index f534296bd0b..230970bed81 100644
--- a/source/blender/editors/screen/screen_edit.c
+++ b/source/blender/editors/screen/screen_edit.c
@@ -1259,6 +1259,7 @@ ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *area, const
     oldscreen = WM_window_get_active_screen(win); /* the one disappearing */
 
     BLI_assert(BKE_workspace_layout_screen_get(layout_old) != screen);
+    BLI_assert(BKE_workspace_layout_screen_get(layout_old) == oldscreen);
     BLI_assert(BKE_workspace_layout_screen_get(layout_old)->state != SCREENNORMAL);
 
     screen->state = SCREENNORMAL;
@@ -1312,6 +1313,8 @@ ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *area, const
      * Without doing so, the screen handling gets wrong area coords,
      * which in worst case can lead to crashes (see T43139) */
     screen->skip_handling = true;
+
+    WM_window_set_active_screen(win, workspace, screen);
   }
   else {
     /* change from SCREENNORMAL to new state */
@@ -1378,7 +1381,11 @@ ScrArea *ED_screen_state_toggle(bContext *C, wmWindow *win, ScrArea *area, const
     ED_screen_change(C, screen);
   }
 
+  BLI_assert(workspace = WM_window_get_active_workspace(win));
+  BLI_assert(win == CTX_wm_window(C));
+
   /* XXX bad code: setscreen() ends with first area active. fullscreen render assumes this too */
+  CTX_wm_screen_set(C, screen);
   CTX_wm_area_set(C, screen->areabase.first);
 
   return screen->areabase.first;
diff --git a/source/blender/editors/screen/screen_ops.c b/source/blender/editors/screen/screen_ops.c
index aff19ddacf1..cc2152d6bfd 100644
--- a/source/blender/editors/screen/screen_ops.c
+++ b/source/blender/editors/screen/screen_ops.c
@@ -3162,7 +3162,8 @@ static void SCREEN_OT_screen_set(wmOperatorType *ot)
 /* function to be called outside UI context, or for redo */
 static int screen_maximize_area_exec(bContext *C, wmOperator *op)
 {
-  bScreen *screen = CTX_wm_screen(C);
+  wmWindow *win = CTX_wm_window(C);
+  bScreen *screen = WM_window_get_active_screen(win);
   ScrArea *area = NULL;
   const bool hide_panels = RNA_boolean_get(op->ptr, "use_hide_panels");
 
@@ -3183,13 +3184,13 @@ static int screen_maximize_area_exec(bContext *C, wmOperator *op)
     if (!ELEM(screen->state, SCREENNORMAL, SCREENFULL)) {
       return OPERATOR_CANCELLED;
     }
-    ED_screen_state_toggle(C, CTX_wm_window(C), area, SCREENFULL);
+    ED_screen_state_toggle(C, win, area, SCREENFULL);
   }
   else {
     if (!ELEM(screen->state, SCREENNORMAL, SCREENMAXIMIZED)) {
       return OPERATOR_CANCELLED;
     }
-    ED_screen_state_toggle(C, CTX_wm_window(C), area, SCREENMAXIMIZED);
+    ED_screen_state_toggle(C, win, area, SCREENMAXIMIZED);
   }
 
   return OPERATOR_FINISHED;
Campbell Barton (campbellbarton) changed the subtype of this task from "Bug" to "Report".

Closing this report as screen_full_area sets a new screen and area which is needed for the second call to works as expected.

This works:

import os.path
import bpy

print("=========BEGIN ====")
str_contextName='VIEW_3D'
bool_found = False
for window in bpy.context.window_manager.windows:
    screen = window.screen
    for area in screen.areas:
        if area.type == str_contextName:
            print(" ==> found context A")
            bpy.context.window.scene = bpy.context.scene
            override = {'window': window, 'screen': screen, 'area': area}
            bpy.ops.screen.screen_full_area(override)
            break
    screen = window.screen
    for area in screen.areas:
        if area.type == str_contextName:
            print(" ==> found context B")
            override = {'window': window, 'screen': screen, 'area': area}
            bpy.ops.screen.screen_full_area(override)
            break
print("=========END====")