FLTK crash when saving preferences

31 views
Skip to first unread message

pvr...@btinternet.com

unread,
Apr 25, 2025, 9:35:31 AM4/25/25
to fltk.general
I have found that when I I delete my Fl_Preferences object or when I call flush() on it, and I have the preferences open in a text editor, I get an exception. I think I had been able to do this successfully in the past, but not sure.

I am using FLTK 1.4.2 on Debian bookworm, with nedit as the text editor.

Regards Phil.

Philip Rose

unread,
Apr 25, 2025, 10:03:26 AM4/25/25
to fltkg...@googlegroups.com

No it's actually something I've done with the data being saved, and I don't know what, yet. It's happened again after I had changed something in my settings. I didn't have the .prefs file open at the time. Anyway the core stack at time of failure as I was closing the app:

Thread 1 "zzalog" received signal SIGABRT, Aborted.
__pthread_kill_implementation (threadid=<optimized out>, signo=signo@entry=6,
    no_tid=no_tid@entry=0) at ./nptl/pthread_kill.c:44
44    ./nptl/pthread_kill.c: No such file or directory.
(gdb) bt
#0  __pthread_kill_implementation
    (threadid=<optimized out>, signo=signo@entry=6, no_tid=no_tid@entry=0)
    at ./nptl/pthread_kill.c:44
#1  0x00007ffff60a9f1f in __pthread_kill_internal
    (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78
#2  0x00007ffff605afb2 in __GI_raise (sig=sig@entry=6)
    at ../sysdeps/posix/raise.c:26
#3  0x00007ffff6045472 in __GI_abort () at ./stdlib/abort.c:79
#4  0x00007ffff609e430 in __libc_message
    (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff61b8459 "%s\n")
    at ../sysdeps/posix/libc_fatal.c:155
#5  0x00007ffff60b383a in malloc_printerr
    (str=str@entry=0x7ffff61b6040 "corrupted size vs. prev_size")
    at ./malloc/malloc.c:5660
#6  0x00007ffff60b40fe in unlink_chunk
    (p=<optimized out>, av=0x7ffff61f1c60 <main_arena>)
    at ./malloc/malloc.c:1623
#7  0x00007ffff60b4280 in malloc_consolidate
    (av=av@entry=0x7ffff61f1c60 <main_arena>) at ./malloc/malloc.c:4776
#8  0x00007ffff60b68a8 in _int_malloc
    (av=av@entry=0x7ffff61f1c60 <main_arena>, bytes=bytes@entry=4096)
    at ./malloc/malloc.c:3961
#9  0x00007ffff60b7989 in __GI___libc_malloc (bytes=bytes@entry=4096)
--Type <RET> for more, q to quit, c to continue without paging--
    at ./malloc/malloc.c:3323
#10 0x00007ffff60948cc in __GI__IO_file_doallocate (fp=0x55555b310030)
    at ./libio/filedoalloc.c:101
#11 0x00007ffff60a20a0 in __GI__IO_doallocbuf (fp=0x55555b310030)
    at ./libio/libioP.h:947
#12 __GI__IO_doallocbuf (fp=fp@entry=0x55555b310030) at ./libio/genops.c:342
#13 0x00007ffff60a1478 in _IO_new_file_overflow (f=0x55555b310030, ch=-1)
    at ./libio/fileops.c:744
#14 0x00007ffff60a063e in _IO_new_file_xsputn
    (n=35, data=<optimized out>, f=0x55555b310030) at ./libio/libioP.h:947
#15 _IO_new_file_xsputn (f=0x55555b310030, data=<optimized out>, n=35)
    at ./libio/fileops.c:1196
#16 0x00007ffff6095b63 in __GI__IO_fwrite
    (buf=0x5555559396f8, size=1, count=35, fp=0x55555b310030)
    at ./libio/libioP.h:947
#17 0x0000555555846893 in Fl_Preferences::RootNode::write()
    (this=0x555555b42500) at /home/philip/dev/fltk/src/Fl_Preferences.cxx:1483
#18 0x0000555555846361 in Fl_Preferences::RootNode::~RootNode()
    (this=0x555555b42500, __in_chrg=<optimized out>)
    at /home/philip/dev/fltk/src/Fl_Preferences.cxx:1395
#19 0x0000555555844c02 in Fl_Preferences::~Fl_Preferences()
    (this=0x555555b42470, __in_chrg=<optimized out>)
    at /home/philip/dev/fltk/src/Fl_Preferences.cxx:576
--Type <RET> for more, q to quit, c to continue without paging--
#20 0x0000555555844c46 in Fl_Preferences::~Fl_Preferences()
    (this=0x555555b42470, __in_chrg=<optimized out>)
    at /home/philip/dev/fltk/src/Fl_Preferences.cxx:582
#21 0x00005555556ecd5e in tidy() () at src/main.cpp:862
#22 0x00005555556ee108 in main(int, char**) (argc=1, argv=0x7fffffffdfc8)
    at src/main.cpp:1220
(gdb) q


--
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 visit https://groups.google.com/d/msgid/fltkgeneral/7d3e5167-9ba1-4da0-a2e0-bbbbe4e0dc3fn%40googlegroups.com.

Matthias Melcher

unread,
Apr 25, 2025, 11:43:34 AM4/25/25
to fltk.general

Do you have  a small piece of source code that can compile by itself and that shows this behavior. It would help me a lot if I can reproduce your crash here. Thank you.

pvr...@btinternet.com

unread,
Apr 25, 2025, 11:54:28 AM4/25/25
to fltk.general



From: 'Matthias Melcher' via fltk.general <fltkg...@googlegroups.com>
Sent: Friday, April 25, 2025 4:43 PM
To: fltk.general <fltkg...@googlegroups.com>
Subject: Re: [fltk.general] FLTK crash when saving preferences
 

Do you have  a small piece of source code that can compile by itself and that shows this behavior. It would help me a lot if I can reproduce your crash here. Thank you.

Sorry, my use of preferences is quite complex, and it's something to do with that. I am also having sporadic malloc fails in other places. I even went as far as rebooting the machine to see if that improved matters. I need to step away from the problem for a while.

Phil.

--

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.

pvr...@btinternet.com

unread,
Apr 26, 2025, 10:44:32 AM4/26/25
to fltk.general
On Friday, April 25, 2025 at 4:54:28 PM UTC+1 pvr...@btinternet.com wrote:



From: 'Matthias Melcher' via fltk.general <fltkg...@googlegroups.com>
Sent: Friday, April 25, 2025 4:43 PM
To: fltk.general <fltkg...@googlegroups.com>
Subject: Re: [fltk.general] FLTK crash when saving preferences
 

Do you have  a small piece of source code that can compile by itself and that shows this behavior. It would help me a lot if I can reproduce your crash here. Thank you.

Sorry, my use of preferences is quite complex, and it's something to do with that. I am also having sporadic malloc fails in other places. I even went as far as rebooting the machine to see if that improved matters. I need to step away from the problem for a while.

I've eliminated it's just the size of the .prefs file by creating a simple program to open the file, read a couple of items and then flush it. This worked fine using the command "fltk-compile -g --compile example.txt" and then "gdb example".

I then decided to dump the whole contents of the prefs file, and it fails on the first call to Fl_Preferences::groups(). I've gone through my code and I can't find that I've made a mistake.

(gdb) r
Starting program: /home/philip/dev/zzatools/test/src/example
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x000055555557bb0e in Fl_Preferences::Node::nChildren (this=0x5555556987b0)
    at /home/philip/dev/fltk/src/Fl_Preferences.cxx:1855
1855     for ( Node *nd = first_child_; nd; nd = nd->next_ )
(gdb) bt
#0  0x000055555557bb0e in Fl_Preferences::Node::nChildren()
    (this=0x5555556987b0) at /home/philip/dev/fltk/src/Fl_Preferences.cxx:1855
#1  0x0000555555578c34 in Fl_Preferences::groups() (this=0x7fffffffdef0)
    at /home/philip/dev/fltk/src/Fl_Preferences.cxx:619
#2  0x0000555555577fa5 in print_prefs(Fl_Preferences) (pref=...)
    at example.cpp:16
#3  0x00005555555780b9 in main(int, char**) (arc=1, argv=0x7fffffffe048)
    at example.cpp:25

I attach the program and the prefs file. I think I've removed e-mail addresses and passwords from it. Also the value returned from the call of path() is not valid.
example.cpp
EXAMPLE.prefs

Matthias Melcher

unread,
Apr 27, 2025, 11:55:01 AM4/27/25
to fltk.general
Thanks for shrinking down the code. This is incredibly helpful.

The Fl_Preferences API is a bit uncommon for classic C++. It tries to ensure that preference databases are always written, even when forgetting to flush or without the need to explicitly write the data. This is done by expecting Preferences to be stack variable, for example.

void write_prefs() {
  Fl_Preferences prefs(...)
  prefs.set(...);
}

When the methods ends, the `prefs` destructor will be called, flushing all the data to the file. Your code is using `new` to allocate the prefs, but no `delete`, so the database would normally not be created, *but*.

But then you call a method, and the method takes `Fl_Preferences pref` as a parameter. This implicitly copies the original `prefs`. There is no copy constructor, so C++ falls back to the default implementation, which will fail. I should have disable the copy constructor, which I will do in 1.5, as this is much easier with C++11.

So all you have to do is:

`void print_prefs(Fl_Preferences pref)` 
  >>> `void print_prefs(Fl_Preferences &pref)`

(note the ampersand) and 

`Fl_Preferences* prefs = new Fl_Preferences(Fl_Preferences::USER_L, "EXAMPLE", "EXAMPLE");`
 >>> `Fl_Preferences prefs(Fl_Preferences::USER_L, "EXAMPLE", "EXAMPLE");`

Now when your app leaves `main`, your prefs will be written automatically.




pvr...@btinternet.com

unread,
Apr 27, 2025, 4:09:21 PM4/27/25
to fltk.general
From: 'Matthias Melcher' via fltk.general <fltkg...@googlegroups.com>
Sent: Sunday, April 27, 2025 4:55 PM

To: fltk.general <fltkg...@googlegroups.com>
Subject: Re: [fltk.general] FLTK crash when saving preferences
Thanks Matthias,

The situation I modelled did not actually represent the situation I originally faced, it was the closest I could get. It failed in an explicit delete of the Fl_Preferences object. I removed the explicit delete and it no longer fails in the app. 

However your answer has pointed me in a direction to replace the Fl_Preferences* object with a pointer to a stack variable, and to check that I always pass lower-level preferences as a reference.

Regards Phil.


--
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/ILti8wmfBq8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to fltkgeneral...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/fltkgeneral/97896b63-a871-47d6-8d80-33366bb8f8ddn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages