Using windows and callbacks to close windows

10 views
Skip to first unread message

John E. Kaye P.Eng.

unread,
Mar 7, 2021, 5:14:15 AM3/7/21
to fltk.general
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.

testwin.tar

Albrecht Schlosser

unread,
Mar 7, 2021, 7:45:03 AM3/7/21
to fltkg...@googlegroups.com
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.
callback.diff
Reply all
Reply to author
Forward
0 new messages