FLTK logo

Re: [fltk.general] Using windows and callbacks to close windows

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Using windows and callbacks to close windows Albrecht Schlosser Mar 07, 2021  
 
On 3/7/21 6:13 AM John E. Kaye P.Eng. wrote:
I am still a newbie here working with FLTK in creating main windows and sub windows. In the attached simple testing code all I want to do is CLOSE a sub window w1 either from a callback OR from an external program. It continually gives a segmentation fault and core dumped. if someone could give me a hand and tell me where I have gone wrong that would great.

Hi John,

I think we had a similar issue recently but I can't remember if it was you or someone else who posted it. Maybe you remember ...

In newTrading.cxx you declare the global variable

  Fl_Window *w1 = 0;

which is fine and in test_Feb20.cxx you declare

 extern Fl_Window *w1;

which is also fine and obviously what you intend because it refers to the global variable 'w1' in newTrading.cxx.

So far, so good, but then you *also* declare

  Fl_Window *w1 = new Fl_Window(...);

inside your main() program which is a new *local* variable and "shadows" the global variable declared above. Hence the *global* variable 'w1' is always null and that causes the segfault.

The only thing you need to do is change the line above in main() to:

  w1 = new Fl_Window(...);

which does /not/ declare a new local variable but refers to (and initializes) the global variable. With this change your program does not crash.

However, I suspect this is not what you try to achieve because you hide your subwindow w1 before it is actually shown on the screen. Note that you can see a window only after it is show()n *and* Fl::run() is executing to show it on the screen.

So, the next logical step is to remove the call

  test_windows();

from your main program and ... it works. The subwindow is shown and you can click the menu and invoke the callback. Try it yourself...

Of course this is still not what you wanted. You wanted to call test_windows() from the main file and have it execute the hide() call on the subwindow using the global variable w1. Well, that's easily done by calling test_windows() in the callback. Hence I replaced:

-  w1->hide();
+  // w1->hide();
+  test_windows();

... now calling test_windows() from Closew1_CB(). I believe this is what you wanted to achieve and I attach my diff file callback.diff that shows all changes so far.

OK, fine, that demonstrates the use of global variable w1 in one cxx file and the extern variable w1 in another file. But let me add a suggestion:

A more elegant way to close a window in a callback or function defined in "another" .cxx file is to use a parameter in that function. I suggest to rewrite

  test_windows(Fl_Window *w1) {...}

to use the parameter w1 and call it from your callback (see my diff file) with this argument - which is better style and avoids the global variables. You can finally delete all 'extern' declarations.

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/f4503bac-46ff-a375-9ff8-d681db17d307%40online.de.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.