Possible bug: GetActiveWindow() returning -1

Just starting out? Need help? Post your questions and find answers here.
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Possible bug: GetActiveWindow() returning -1

Post by coco2 »

I'm having some difficulty with GetActiveWindow(). When I run my application a #PB_Event_ActivateWindow is triggered and I naturally try to obtain which window is active by calling GetActiveWindow(). Occasionally it will return -1 meaning there are no active windows. This is an intermittant problem and I have seen it on both Windows and Linux. When the debug window is opened it seems to increase the chances of the problem happening but I have seen the problem without a debug window.

Is this an intermittent bug or am I just not understanding PureBasic right. Should I be using GetActiveWindow() differently?

Code: Select all

For c = 1 To 1000
  ShowDebugOutput()
  Debug "Count: " + c
  OpenWindow(0,100, 100, 640, 480, "Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)
  Repeat
    Event = WaitWindowEvent(10)
    Select Event
      Case #PB_Event_MoveWindow
        Debug "Window moved"
      Case #PB_Event_DeactivateWindow
        Debug "Window deactivated"
      Case #PB_Event_ActivateWindow
        Debug "A window has been activated"
        w = GetActiveWindow()
        If w = -1 
          Debug "Yet there are no active windows"
          End
        Else
          Debug "Active window: " + w
        EndIf
      Case #PB_Event_CloseWindow
        Quit = 1
    EndSelect
  Until Event = 0
  CloseWindow(0)
  CloseDebugOutput()
Next
If you comment out ShowDebugOutput() and CloseDebugOutput() it will still have the problem although very rarely.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Possible bug: GetActiveWindow() returning -1

Post by srod »

Why not simply use EventWindow() to get the ID of the relevant window?
I may look like a mule, but I'm not a complete ass.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Possible bug: GetActiveWindow() returning -1

Post by Dude »

Hmm, you may be onto something. Here's smaller code which shows the problem.

It seems ShowDebugOutput() is causing the error, which I wouldn't have expected. :shock:

Code: Select all

ShowDebugOutput() ; Remove this to see the difference.

OpenWindow(0,100, 100, 640, 480, "Test", #PB_Window_SystemMenu)

Repeat
  Event = WaitWindowEvent()
  Select Event
    Case #PB_Event_ActivateWindow
      Debug "A window has been activated"
      w = GetActiveWindow()
      If w = -1
        Debug "Yet there are no active windows"
        End
      Else
        Debug "Active window: " + w
      EndIf
    Case #PB_Event_CloseWindow
      Quit = 1
  EndSelect
Until Quit
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Possible bug: GetActiveWindow() returning -1

Post by freak »

Code: Select all

      Case #PB_Event_ActivateWindow
        Debug "A window has been activated"
        w = GetActiveWindow()
The "Debug" command activates the debugger window which is why the program loses focus. The GetActiveWindow() result correctly reflects this.

The GetActiveWindow() command returns the window state as it is now. The #PB_Event_ActivateWindow event comes from the queue which means it can already be out of date (another #PB_Event_DeactivateWindow can already be waiting in the event queue)

The solution is to use EventWindow() which returns the window that actually caused the event that is currently being processed.
quidquid Latine dictum sit altum videtur
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Possible bug: GetActiveWindow() returning -1

Post by coco2 »

I changed my test code and you are right it does have an additional deactivate event waiting.

The solution is what I suspected, you have to be careful using GetActiveWindow() because it can change at any time.
Post Reply