seg fault with fl_draw fltk1.4.4

16 views
Skip to first unread message

Klaus Rudolph

unread,
Oct 26, 2025, 10:28:41 AM (12 days ago) Oct 26
to fltk.general
I try to build the following prog:

class MyWin: public Fl_Window
{
    public:
        MyWin( int x, int y, int w, int h, const char* title = 0 ):
            Fl_Window(x,y,w,h,"Hallo Welt!")
    {  
    }  

        void draw() override
        {
            Fl_Window::draw(); // Das zeichnet den Background neu, falls erforderlich!
            std::cout << "Draw!" << std::endl;
            fl_color( FL_WHITE );
            fl_draw( "Hallo world", 10,20);
        }
};


int main() {
    auto display = new MyWin(1,1,900, 600, "Test");
    display->color(FL_RED);
    display->show();

    Fl::run();
}


I compiled it with:
    g++ -g \
        `/home/krud/external_repos/fltk-1.4.4/myinstall/bin/fltk-config --cflags` \
        main.cpp \
        `/home/krud/external_repos/fltk-1.4.4/myinstall/bin/fltk-config --ldflags` \
        -o go

I got:
Program received signal SIGSEGV, Segmentation fault.

backtrace in gdb:
#0  0x000000000043d4fd in Fl_Cairo_Graphics_Driver::draw (this=0x58fce0, str=0x4f969a "Hallo world", n=11, x=10, y=20) at drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx:1343
#1  0x0000000000437913 in Fl_Cairo_Graphics_Driver::draw (this=0x58fce0, s=0x4f969a "Hallo world", nBytes=11, x=10, y=20) at drivers/Cairo/Fl_Cairo_Graphics_Driver.H:170
#2  0x0000000000426308 in fl_draw (str=0x4f969a "Hallo world", n=11, x=10, y=20) at ../FL/fl_draw.H:957
#3  0x0000000000428b57 in fl_draw (str=0x4f969a "Hallo world", x=10, y=20) at fl_font.cxx:33
#4  0x00000000004024a4 in MyWin::draw (this=0x565380) at main.cpp:30
#5  0x000000000041f5a2 in Fl_Window::flush (this=0x565380) at Fl_Window.cxx:487
#6  0x0000000000420db1 in Fl_Window_Driver::flush (this=0x54cf50) at Fl_Window_Driver.cxx:190
#7  0x000000000040304c in Fl::flush () at Fl.cxx:820
#8  0x000000000046c858 in Fl_Unix_System_Driver::wait (this=0x54c240, time_to_wait=1e+20) at drivers/Unix/Fl_Unix_System_Driver.cxx:803
#9  0x0000000000402d79 in Fl::wait (time_to_wait=1e+20) at Fl.cxx:632
#10 0x0000000000402d93 in Fl::run () at Fl.cxx:652
#11 0x0000000000402310 in main () at main.cpp:40

Any idea what I did wrong?

Albrecht Schlosser

unread,
Oct 26, 2025, 11:18:06 AM (12 days ago) Oct 26
to fltkg...@googlegroups.com
Hi Klaus,

this group is moderated, at least for first-time posters. Please wait some time for your post to appear. Thanks.
A moderator may enable you to post w/o moderation at some time...


On 10/26/25 15:13 'Klaus Rudolph' via fltk.general wrote:
I try to build the following prog:

For your next questions, please post the full program with all headers so we can compile and test. At a first glance I didn't see any error, but ...

class MyWin: public Fl_Window
{
    public:
        MyWin( int x, int y, int w, int h, const char* title = 0 ):
            Fl_Window(x,y,w,h,"Hallo Welt!")
    {  
    }  

        void draw() override
        {
            Fl_Window::draw(); // Das zeichnet den Background neu, falls erforderlich!
            std::cout << "Draw!" << std::endl;
            fl_color( FL_WHITE );

Before you can draw text you need to set a font (at least once, but it is recommended to do it always immediately before drawing text to guarantee consistent behavior (otherwise any previously set font might be used). Add the following line before fl_draw to prevent the segfault:

fl_font(FL_COURIER, 20);
Of course any other font would do as well.


            fl_draw( "Hallo world", 10,20);
        }
};


int main() {
    auto display = new MyWin(1,1,900, 600, "Test");
    display->color(FL_RED);
    display->show();

    Fl::run();
}


I compiled it with:
    g++ -g \
        `/home/krud/external_repos/fltk-1.4.4/myinstall/bin/fltk-config --cflags` \
        main.cpp \
        `/home/krud/external_repos/fltk-1.4.4/myinstall/bin/fltk-config --ldflags` \
        -o go

I got:
Program received signal SIGSEGV, Segmentation fault.

backtrace in gdb:

[elided]

Klaus Rudolph

unread,
Oct 26, 2025, 1:31:44 PM (12 days ago) Oct 26
to fltk.general
Albrecht-S schrieb am Sonntag, 26. Oktober 2025 um 16:18:06 UTC+1:
Hi Klaus,

this group is moderated, at least for first-time posters. Please wait some time for your post to appear. Thanks.
A moderator may enable you to post w/o moderation at some time...


Sorry, I will wait next time!

>>  fl_font(FL_COURIER, 20);

Yep, my prog works now! Perfect!

Maybe it should be mentioned in the docs that some font has to be set before. Interestingly with 1.3.3 it has worked without setting the font.

Regards
 Klaus

Albrecht Schlosser

unread,
Oct 26, 2025, 2:15:32 PM (12 days ago) Oct 26
to fltkg...@googlegroups.com
On 10/26/25 16:43 'Klaus Rudolph' wrote:

Albrecht-S schrieb am Sonntag, 26. Oktober 2025 um 16:18:06 UTC+1:
Hi Klaus,

this group is moderated, at least for first-time posters. Please wait some time for your post to appear. Thanks.
A moderator may enable you to post w/o moderation at some time...


Sorry, I will wait next time!

No problem. You are now enabled to post anyway. ;-)


>>  fl_font(FL_COURIER, 20);

Yep, my prog works now! Perfect!

Great, thanks for the feedback.


Maybe it should be mentioned in the docs that some font has to be set before.

Good point. Someone (maybe not me) should do this...


Interestingly with 1.3.3 it has worked without setting the font.

This *may* be an unintended side effect *or* it may be a platform specific side effect. For instance, it's possible that the X11 platform code sets a default font (e.g. in 1.3.x) and that your code used Wayland - where no default font is set - in 1.4. Note that Wayland is the new default, if present.

Anyway, it's not guaranteed, and you should always set a font at least once at the beginning of any draw() method to ensure that this font is used and not an arbitrary font that has been set previously. Imagine that this can be random: it would be the last font that's been used in the widget drawn last before the widget in question.

Reply all
Reply to author
Forward
0 new messages