Window closes unexpectedly

Just starting out? Need help? Post your questions and find answers here.
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Window closes unexpectedly

Post by eck49 »

What could be causing this?

This is using the 5.70 IDE (I can't install 5.73) in Linux

I'm writing a project which already has a few thousand lines of code which neither need a window nor an event loop - reading in and checking data.
Now I open a window (no event loop just yet), but as soon as I try to create a gadget the window closes. The minimal code snippet below behaves like this. I have tried opening an old working project, creating a new file (without adding it to the project) and pasting this code in and it does it there too.

Code: Select all

OpenWindow(100, 10, 50, 500, 500, "Heading",
             #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_TitleBar)
  
  
  ;window colour
  SetWindowColor(100, $ffa0a0)
  
  ;hide window while initially painting
  HideWindow(100, 1)		;but see below
  
      TextGadget(200, 5,5, 200,30, "Testing", 0)
When I step through (F8) the code, the window closes on executing the TextGadget statement. It isn't just hidden, checking for a window event raises an error. It's not just TextGadget, same applies for a StringGadget.

But if I comment out the HideWindow, the window behaves just as it should.


If I add

Code: Select all

 HideWindow(100, 0)
after the TextGadget call, I see a transparent ghost version of the window with just a border and titlebar, so maybe it isn't fully closed.

In past projects I have usually hidden a window while it is being populated with gadgets - using code just like this - so what is stopping me doing it here?
Have I made an elementary error - as usual?
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
BarryG
Addict
Addict
Posts: 3294
Joined: Thu Apr 18, 2019 8:17 am

Re: Window closes unexpectedly

Post by BarryG »

eck49 wrote: Mon Jan 17, 2022 12:36 amThe minimal code snippet below behaves like this
But the minimal code snippet provided isn't runnable, so we can't see the problem. You can definitely hide a window while adding gadgets; I do it all the time.

To me, it sounds like you're trying to add a gadget followed by doing something to that gadget with an incorrect ID, so that it causes an illegal memory access error. Like, adding it and then setting a tooltip for it with an invalid gadget number or something. Or maybe UseGadgetList() is being used but set to a non-existent window or something?

Without more code, we just can't say.
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Window closes unexpectedly

Post by Marc56us »

Tips:

Instead of

Code: Select all

OpenWindow(
HideWindow(
Uses

Code: Select all

OpenWindow( ... | #PB_Window_Invisible
... create gadgets ...
... then unhide window (You will not have ghost window)
HideWindow(... , #False)

And avoid using direct numeric values as gadget/filehandle etc ID. We always end up with problems. It only takes a simple mistake of typing '100' instead of '1000' for nothing to work and it's very difficult to find.
Uses Enumeration / EndEnumeration or #PB_Any

(and of course use EnableExplicit even if it makes more code to type (obligation to declare all variables, it avoids typing errors. )

:wink:
User avatar
mk-soft
Always Here
Always Here
Posts: 5337
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Window closes unexpectedly

Post by mk-soft »

What could be causing this?

This is using the 5.70 IDE (I can't install 5.73) in Linux

I'm writing a project which already has a few thousand lines of code which neither need a window nor an event loop - reading in and checking data.
Now I open a window (no event loop just yet), but as soon as I try to create a gadget the window closes. The minimal code snippet below behaves like this. I have tried opening an old working project, creating a new file (without adding it to the project) and pasting this code in and it does it there too.
Use the Purebasic IDE from PB v5.73. Just replace the IDE, not the compiler and libraries.
In the version the problem with the crashes of the IDE is eliminated.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Window closes unexpectedly

Post by Marc56us »

I'm writing a project which already has a few thousand lines of code which neither need a window nor an event loop - reading in and checking data.
So a console program
Now I open a window (no event loop just yet), but as soon as I try to create a gadget the window closes.
Have I made an elementary error - as usual?
If you don't add a loop in a GUI program, the window close himself when at end. This is the normal way.
A GUI program need a loop to stay open.

Minimal GUI PB program:

Code: Select all

OpenWindow(100, 10, 50, 500, 500, "")
; Minimal loop (add this to models)
Repeat : Until WaitWindowEvent(0) = #PB_Event_CloseWindow
:wink:
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Window closes unexpectedly

Post by eck49 »

BarryG, Marc56us, mk-soft

Thanks for all your replies. It really is appreciated.

BarryG: I can run it most easily by piggybacking on another project. i've used this technique a lot to try out stand-alone code snippets from the forum without modifying the main project. Open any project in the IDE, then do File>New. Be careful NOT to add the file to the project at any point. Paste in the snippet then F5 will run it. Of course, you will need to insert a breakpoint or a loop to interrupt the flow or it will finish before you can see what is going on!

I've fallen into all the traps you mention before now - but not this time. This is the minimal code which shows the issue.

Marc56us: I'd not come across #PB_Window_Invisible before. I'll experiment. On direct numbers, I do use enumerations normally but stripped them out of this tiny example for simplicity. And EnableExplicit! I agree, the extra typing is well worth the effort.

mk-soft: Where in the 5.73 package is the IDE? I'd like to try it, without compromising the working 5.70 version. Can you give me a clue?
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Window closes unexpectedly

Post by eck49 »

@Marc56us: Just seen your 10:30 reply, thanks.

No, not really a console project. Before opening a window, the program reads a bunch of files which set hundreds of parameters for the program and checks them for consistency. No user interaction needed for this.

In the real project, there is indeed a loop of the kind you suggest, after the OpenWindow call. When, early in the loop there is code which expects an open window, namely

Code: Select all

mEvent = WaitWindowEvent()
I get an error saying there was no window open. By stripping back the code between the program start and the loop, I arrived at the fragment of code in my first post. In that fragment, I put a breakpoint on the OpenWindow call then plod through line by line, which is how I know it is on the TextGadget call the window is lost when the HideWindow is present and not lost when it is commented.

What is particularly odd is that just a few days ago this part of the code was working as I expected. Unfortunately I can't revert to that point without a great deal of work as I never anticipated an issue with it.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: Window closes unexpectedly

Post by Marc56us »

It is as if the main window had been closed instead of hidden. What is strange is that this window is unique, closing it should have closed the program too (?)

One thing to do: Prefix the first gadget creation command with a test for the presence of the main window (if Not IsWindow(...) : MessageBox ("Error... ) : end : EndIf...)

In general, this type of program is standardized as follows (what the user sees is not what the program does)

1. Open the main window in hidden mode because it serves as a reference for everything else.
2. Open a second window in the middle (SplashScreen) with some tricks to make the user wait "Please wait..." "Initialize Data" and better yet, a progress bar. We put this window above the others (StickyWindow(...))
3. Initialize the datas, load files etc
4. Build the continuation of the main window
5. Unhide the main window
6 Delete the SplashScreen (use CloseWindow(), not HideWindow()
7. Start the main loop
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Window closes unexpectedly

Post by eck49 »

Marc56us:
Tested as you suggest. IsWindow returns True before the call to TextGadget and it is only when TextGadget is called that the window disappears.
After the call to TextGadget, if I test again, IsWindow returns true and if I then unhide the window I see a ghost.

If I then run this having commented out the HideWindow, I note that the window colour only changes from the default grey after the gadget creation.

Just to see what happens, I added

Code: Select all

  Repeat
Until WaitWindowEvent(0) = #PB_Event_CloseWindow    
below the code, and this complete version (using Debug rather than MessageBox - lazy me!)

Code: Select all

OpenWindow(100, 10, 50, 500, 500, "Heading", 
             #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_TitleBar)
  
  
  ;window colour
  SetWindowColor(100, $ffa0a0)
  
  ;hide window while initially painting
  HideWindow(100, 1)
  
  If IsWindow(100) 
    TextGadget(200, 5,5, 200,30, "Testing Yes", 0)
    If IsWindow(100) : Debug "Two hidden? It is" : EndIf
    HideWindow(100, 0)
  Else
    Debug "Three Gone"
    TextGadget(200, 5,5, 200,30, "Testing Bad", 0)
  EndIf  
  Repeat
Until WaitWindowEvent(0) = #PB_Event_CloseWindow    
 End
now behaves exactly as I would have expected originally.

Could this be a matter of timing? Perhaps - depending on what other tasks the computer has running, not just associated with PB - in the absence of any way of catching events (for example, the loop) PB could be caught out by competing actions at machine code level being started or completed out of order? If that is the case, it could lead, in very rare and virtually untestable circumstances, to inconsistent behaviour like this now appears to me to be. I can believe that the loop gives PB the chance to get its ducks in a row, although it still leaves a loose end or two for me.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Window closes unexpectedly

Post by netmaestro »

This behaves as expected with an event loop added:

Code: Select all

OpenWindow(100, 10, 50, 500, 500, "Heading",
           #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_TitleBar)


;window colour
SetWindowColor(100, $ffa0a0)

;hide window while initially painting
HideWindow(100, 1)		;but see below

TextGadget(200, 5,5, 200,30, "Testing", 0)

Repeat:
  WindowEvent()
Until GetAsyncKeyState_(#VK_SPACE)

HideWindow(100, 0)

Repeat:
  WindowEvent()
Until GetAsyncKeyState_(#VK_ESCAPE)
But it's on beta 2 for windows, not linux.
Btw if someone hasn't pointed it out, starting static window/gadget numbers at 100 is a waste of space as room for 99 other objects gets reserved. If someone already mentioned it, then I didn't, please unread my last line.
BERESHEIT
Post Reply