FLTK usage in passing pointers from file to file

19 views
Skip to first unread message

John E. Kaye P.Eng.

unread,
Feb 23, 2021, 10:53:30 PM2/23/21
to fltk.general
I am a newbie here and have a simple problem that I don't seem to be able to resolve.
Given two(2) separate files that will be compiled and linked together.
In file 1 several windows are opened in the main().  All the windows are defined globally. Example 
Fl_Window *win;
Fl_Window *w1;
Fl_Window *w2;
I define and show() main window "win" and also define and show() "w1" in the main() program in File 1.  I define but hide() "w2" in File 1.
In File 2 I declare the following globally
extern Fl_Window *win;
extern Fl_Window *w1;
extern Fl_Window *w2;. 
Now in File 2 I wish to hide() "w1"[w1,hide()] and show() "w2"[w2.show()]
After a successful compile and link it gives a segmentation fault(core dumped) response?
Can anyone comment on how to globally pass defined windows from file to file?

Greg Ercolano

unread,
Feb 23, 2021, 10:59:52 PM2/23/21
to fltkg...@googlegroups.com
    That should work, but if you're getting a coredump, sounds like maybe the variables
    haven't actually been initialized before they're used.. check to make sure the globals
    are being set before being used, and that the initialization is not somehow a local declaration
    that goes out of scope.

    If you're still not sure, show us a simple two file example that doesn't work but should,
    where one file has the main() and sets the variables, and calls functions in the other
    file that tries to use the variables. There may be something about what you're doing
    in the actual code.

John E. Kaye P.Eng.

unread,
Feb 25, 2021, 8:40:56 PM2/25/21
to fltk.general
Thank you for your response and comment. I had declared the Window pointers Globally such as Fl_Window *w1, Fl_Window *w2 etc BUT defined them in the main() program in FILE 1 as Fl_Window w1(5,198,600,275,"Tester"), etc
So when I now, as you counciled, declare all the Windows globally such as 
Fl_Window w1(5,198,600,275,"Tester");
Fl_Window w2(5,190,600,25,"Test Block");
etc

AND just declare the Windows in the second file FILE 2 as
extern Fl_Window w1;
extern Fl_Window w2;
etc

The Windows for w1 and w2 show() or hide() correctly.  However when the program ends or closes many lines of code are displayed starting with the statement  
*** Error in ./test1pgm : free(): invalid pointer: 0x0000000000000002320
followed by a back trace
BUT, as I stated the program works
Any further comments??

Greg Ercolano

unread,
Feb 25, 2021, 9:12:24 PM2/25/21
to fltkg...@googlegroups.com


On 2/25/21 5:19 PM, John E. Kaye P.Eng. wrote:
Thank you for your response and comment. I had declared the Window pointers Globally such as Fl_Window *w1, Fl_Window *w2 etc BUT defined them in the main() program in FILE 1 as Fl_Window w1(5,198,600,275,"Tester"), etc

    This is bad; sounds like you're declaring two different w1 variables; one global.

    Functions outside main() will only see the uninitialized global pointer variable 'w1',
    not the initialized one inside main().

    Let's ignore w2, and just focus on w1.  Sounds like you have this:

Fl_Window *w1;          // OK: declares global pointer variable w1
int main() {
    Fl_Window w1(...);  // BAD: declares a separate local variable w1 that shadows the global one
    ..
}

     Other functions will only see the global 'w1' which is uninitialized by your code,
     and is causing the crashes.

     What you should do instead is only declare 'w1' once, as a global,
     then set it in main() using the code in green:

// main.cxx
Fl_Window *w1 = 0;   // declare global pointer w1, init to null

int main(..) {
    // Create window w1, saving pointer to created window in w1
    w1 = new Fl_Window(5,198,600,275,"Tester");

    // define widgets that go inside w1..

    w1->end();

   // From here, you can call your functions in your other file
   // that use w1 and it should work

   return Fl::run();  // application loop
}

    That should work correctly.

Any further comments??

    Yes, be careful of variable scoping in C and C++.

    It's possible to have many variables that are completely different, but all
    have the same name, so be careful of that. I believe that's what bit you here.

    And when working with pointers to FLTK widgets, you want to create the widgets
    with this pattern:

        Fl_Some_Widget *wa = new Fl_Some_Widget(...);
        Fl_Other_Widget *wb = new Fl_Other_Widget(...);

   Note the use of 'new' to create an instance of the widget's class, and saving its
   value in the pointer.

    Some widgets are containers, like windows and groups, so you need to use
    begin() and end() when creating widgets that go inside those containers.

    I suggest having a look at this beginner's tutorial for programming in FLTK:
    https://www.youtube.com/watch?v=vW626w2Ipyc

    ..and also how to use 'fluid', the gui interface designer:

    https://www.youtube.com/watch?v=U83xH1KZ_is


lifeatt...@gmail.com

unread,
Feb 26, 2021, 7:37:45 AM2/26/21
to fltk.general
>So when I now, as you counciled, declare all the Windows globally such as 
>Fl_Window w1(5,198,600,275,"Tester");

This is why we want to see a program, not "snippets" with text. We will "guess" what you're doing and be at odds ...

I interpreted what you did differently from Greg.  I think you did:

File1:

Fl_Window w1(5,198,600,275,"Tester"); // note: declared and created globally
main() {
   w1.show()
}

File2:
extern Fl_Window w1;

some_func() {
    w1.hide()
}

To me, the issue I think I'm seeing is you are trying to use "objects" and not pointers. I think the error you are getting at program shutdown is FLTK trying to destroy the FL_Window instances you created globally, and that can't be done. FLTK is a C library, not a C++ library, so it's best if you work with pointers.

The approach I would use is:

File1:
Fl_Window *w1;
main() {
  w1 = new Fl_Window(5, 198, 600, 275, "Tester");  // note: declared globally, allocated ON THE HEAP
  w1->hide(); // pointer syntax
}

File2:
extern Fl_Window *w1; // extern pointer

some_func() {
  w1->hide(); // pointer syntax
}

Kevin

Bill Spitzak

unread,
Feb 26, 2021, 2:21:01 PM2/26/21
to fltkg...@googlegroups.com
The objects should work in your first example. There may be a problem with the destructor assuming other things have not been destroyed.
Generally though when using objects it works better if they are local or local static variables in functions, this defers the constructor until they are first used. Something like this is supposed to work:

main() {
    Fl_Window w1(....);
    some_func(w1);
}

some_func(Fl_Window& w) {
   w.show();
}


--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/ca5666eb-c141-4048-8742-9b91d3e137fcn%40googlegroups.com.

John E. Kaye P.Eng.

unread,
Feb 26, 2021, 8:44:48 PM2/26/21
to fltkg...@googlegroups.com
Regarding passing window "pointers" from one file to another, I must thank you for assisting me in this endeavour. It works well now that I understand the use of "new", begin() and end(). I do note that the text expression in each instance of my declared windows only appears for the first window....or I may be missing something.  Is there a way to easily have a text at the top of each window which would describe the data that will be displayed?  I did look at Fl_Text_Display but it seemed rather to be an overkill.
It would be good to hear your thoughts.  Take care.



You received this message because you are subscribed to a topic in the Google Groups "fltk.general" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/fltkgeneral/uDe3loc7qBY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to fltkgeneral...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/CAL-8oAjD%2BNPNzsHzUrYJFdwdnXPevsgtqKKXhEytn5LZCe%2BGrQ%40mail.gmail.com.

Philip Rose

unread,
Feb 27, 2021, 3:33:20 AM2/27/21
to fltkg...@googlegroups.com

From: John E. Kaye P.Eng.
Sent: 27 February 2021 01:44
To: fltkg...@googlegroups.com
Subject: Re: [fltk.general] FLTK usage in passing pointers from file to file

 

Regarding passing window "pointers" from one file to another, I must thank you for assisting me in this endeavour. It works well now that I understand the use of "new", begin() and end(). I do note that the text expression in each instance of my declared windows only appears for the first window....or I may be missing something.  Is there a way to easily have a text at the top of each window which would describe the data that will be displayed?  I did look at Fl_Text_Display but it seemed rather to be an overkill.

It would be good to hear your thoughts.  Take care.


Hi John,

 

You can set the label of the window. This will put the text into the top bar. If the text is a constant use Fl_Window::label() otherwise use Fl_Window::copy_label().

 

Phil.

imm

unread,
Feb 27, 2021, 3:43:09 AM2/27/21
to general fltk
On Sat, 27 Feb 2021, 01:44 John E. Kaye  wrote:
Regarding passing window "pointers" from one file to another, I must thank you for assisting me in this endeavour. It works well now that I understand the use of "new", begin() and end(). I do note that the text expression in each instance of my declared windows only appears for the first window....or I may be missing something.  Is there a way to easily have a text at the top of each window which would describe the data that will be displayed?  I did look at Fl_Text_Display but it seemed rather to be an overkill.
It would be good to hear your thoughts.  Take care.


I think it would help all of us if you could post here a small, compileable, example of what you have done, so we could see for ourselves...

Did you see the example I posted earlier? Did that work when you tried it?

An example like that from you would help us comprehend what you are doing in your code.

There may also be an issue of terminology at work here; there are certain terms used to refer to GUI elements that are normal and expected for the folk in this forum, but which may be unfamiliar to you 

For example, when you refer to "the text expression in each instance of my declared windows" I'm not sure at all what you mean - do you mean the window title, for example? Or something else?

So a suitably commented example might clarify things - code is (usually!) less ambiguous than speech!

-- 
Ian
From my Fairphone FP3

Reply all
Reply to author
Forward
0 new messages