DEV Community

Discussion on: I'm an Expert in Memory Management & Segfaults, Ask Me Anything!

 
kyrlon profile image
kyrlon

Alright the first block is a gdb output with a backtrace:


gdb ./a.out                                                                                                                                                                                       [0/0]
GNU gdb (Ubuntu 9.1-0ubuntu1) 9.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...
(gdb) run
Starting program: /home/red/recorder/sidekiq/a.out
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
this is the current epoch:  1598981433
Waiting for pps epoch
Info: configured 1PPS source to 1
[New Thread 0x7ffff54cd700 (LWP 4245)]
[New Thread 0x7ffff4ccc700 (LWP 4246)]
Info: starting 0 Rx handle(s) for card 0 on next 1PPS pulse
Info: streaming started on card 0
Error: timestamp error for card 0 handle A1 (blk 0) ... expected 0x0000000000000000 but got 0x0000000000000004 (delta 4)
Info: all 0 card(s) started streaming
Info: sleeping for 3 seconds
Info: signaling to all 1 card(s) to stop streaming
Info: waiting for control thread for card 0
Info: stopping 0 Rx handle(s) on card 0 on next 1PPS pulse
Info: streaming stopped on card 0
double free or corruption (out)
Info: waiting for receive thread for card 0
[Thread 0x7ffff4ccc700 (LWP 4246) exited]

Thread 2 "a.out" received signal SIGABRT, Aborted.
[Switching to Thread 0x7ffff54cd700 (LWP 4245)]
__GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
50      ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007ffff74f9859 in __GI_abort () at abort.c:79
#2  0x00007ffff75643ee in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff768e285 "%s\n") at ../sysdeps/posix/libc_fatal.c:155
#3  0x00007ffff756c47c in malloc_printerr (str=str@entry=0x7ffff7690670 "double free or corruption (out)") at malloc.c:5347
#4  0x00007ffff756e120 in _int_free (av=0x7ffff76bfb80 <main_arena>, p=0x7ffff0000b50, have_lock=<optimized out>) at malloc.c:4314
#5  0x0000555555566af0 in __gnu_cxx::new_allocator<int>::deallocate (this=<synthetic pointer>, __p=0x7ffff0000b60) at /usr/include/c++/9/ext/new_allocator.h:119
#6  std::allocator_traits<std::allocator<int> >::deallocate (__a=<synthetic pointer>..., __n=<optimized out>, __p=0x7ffff0000b60) at /usr/include/c++/9/bits/alloc_traits.h:470
#7  std::_Vector_base<int, std::allocator<int> >::_M_deallocate (this=<synthetic pointer>, __n=<optimized out>, __p=0x7ffff0000b60) at /usr/include/c++/9/bits/stl_vector.h:351
#8  std::_Vector_base<int, std::allocator<int> >::~_Vector_base (this=<synthetic pointer>, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/stl_vector.h:332
#9  std::vector<int, std::allocator<int> >::~vector (this=<synthetic pointer>, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/stl_vector.h:680
#10 sidekiq_class::receive_data (card=0 '\000', p_rconfig=<optimized out>) at src/testing_cpp.cpp:598
#11 0x0000555555567cf8 in sidekiq_class::receive_run (data=<optimized out>) at src/testing_cpp.cpp:705
#12 0x00007ffff7a27609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#13 0x00007ffff75f6103 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
Thread Thread
 
kyrlon profile image
kyrlon

This is the valgrind dump (sorry for double comment; couldn't get the markdown to capture all of the code cleanly):



valgrind --leak-check=yes --track-origins=yes ./a.out
==4364== Memcheck, a memory error detector
==4364== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4364== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==4364== Command: ./a.out
==4364==
Error: unable to initialize libsidekiq with status -16
==4364==
==4364== HEAP SUMMARY:
==4364==     in use at exit: 0 bytes in 0 blocks
==4364==   total heap usage: 5 allocs, 5 frees, 75,144 bytes allocated
==4364==
==4364== All heap blocks were freed -- no leaks are possible
==4364==
==4364== For lists of detected and suppressed errors, rerun with: -s
==4364== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
red@red-NUC8v5PNK:~/recorder/sidekiq$ valgrind --leak-check=yes --track-origins=yes ./a.out
==4367== Memcheck, a memory error detector
==4367== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4367== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==4367== Command: ./a.out
==4367==
this is the current epoch:  1598982515
Waiting for pps epoch
Info: configured 1PPS source to 1
Info: starting 0 Rx handle(s) for card 0 on next 1PPS pulse
Info: streaming started on card 0
==4367== Thread 2:
==4367== Conditional jump or move depends on uninitialised value(s)
==4367==    at 0x11B078: sidekiq_class::update_rx_stats(sidekiq_class::rx_stats*, skiq_rx_block_t*) (testing_cpp.cpp:569)
==4367==    by 0x11B95F: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:646)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Conditional jump or move depends on uninitialised value(s)
==4367==    at 0x11B964: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:647)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Conditional jump or move depends on uninitialised value(s)
==4367==    at 0x11B97C: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:657)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Use of uninitialised value of size 8
==4367==    at 0x51AB7BA: _itoa_word (_itoa.c:180)
==4367==    by 0x51C76F4: __vfprintf_internal (vfprintf-internal.c:1687)
==4367==    by 0x51CA021: buffered_vfprintf (vfprintf-internal.c:2377)
==4367==    by 0x51C6EA3: __vfprintf_internal (vfprintf-internal.c:1346)
==4367==    by 0x527E022: __fprintf_chk (fprintf_chk.c:33)
==4367==    by 0x11B9CD: fprintf (stdio2.h:100)
==4367==    by 0x11B9CD: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:662)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Conditional jump or move depends on uninitialised value(s)
==4367==    at 0x51AB7CC: _itoa_word (_itoa.c:180)
==4367==    by 0x51C76F4: __vfprintf_internal (vfprintf-internal.c:1687)
==4367==    by 0x51CA021: buffered_vfprintf (vfprintf-internal.c:2377)
==4367==    by 0x51C6EA3: __vfprintf_internal (vfprintf-internal.c:1346)
==4367==    by 0x527E022: __fprintf_chk (fprintf_chk.c:33)
==4367==    by 0x11B9CD: fprintf (stdio2.h:100)
==4367==    by 0x11B9CD: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:662)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Conditional jump or move depends on uninitialised value(s)
==4367==    at 0x51C83A8: __vfprintf_internal (vfprintf-internal.c:1687)
==4367==    by 0x51CA021: buffered_vfprintf (vfprintf-internal.c:2377)
==4367==    by 0x51C6EA3: __vfprintf_internal (vfprintf-internal.c:1346)
==4367==    by 0x527E022: __fprintf_chk (fprintf_chk.c:33)
==4367==    by 0x11B9CD: fprintf (stdio2.h:100)
==4367==    by 0x11B9CD: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:662)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Conditional jump or move depends on uninitialised value(s)
==4367==    at 0x51C786E: __vfprintf_internal (vfprintf-internal.c:1687)
==4367==    by 0x51CA021: buffered_vfprintf (vfprintf-internal.c:2377)
==4367==    by 0x51C6EA3: __vfprintf_internal (vfprintf-internal.c:1346)
==4367==    by 0x527E022: __fprintf_chk (fprintf_chk.c:33)
==4367==    by 0x11B9CD: fprintf (stdio2.h:100)
==4367==    by 0x11B9CD: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:662)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Conditional jump or move depends on uninitialised value(s)
==4367==    at 0x51C7AD8: __vfprintf_internal (vfprintf-internal.c:1687)
==4367==    by 0x51CA021: buffered_vfprintf (vfprintf-internal.c:2377)
==4367==    by 0x51C6EA3: __vfprintf_internal (vfprintf-internal.c:1346)
==4367==    by 0x527E022: __fprintf_chk (fprintf_chk.c:33)
==4367==    by 0x11B9CD: fprintf (stdio2.h:100)
==4367==    by 0x11B9CD: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:662)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Use of uninitialised value of size 8
==4367==    at 0x51AB81B: _itoa_word (_itoa.c:179)
==4367==    by 0x51C76F4: __vfprintf_internal (vfprintf-internal.c:1687)
==4367==    by 0x51CA021: buffered_vfprintf (vfprintf-internal.c:2377)
==4367==    by 0x51C6EA3: __vfprintf_internal (vfprintf-internal.c:1346)
==4367==    by 0x527E022: __fprintf_chk (fprintf_chk.c:33)
==4367==    by 0x11B9CD: fprintf (stdio2.h:100)
==4367==    by 0x11B9CD: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:662)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
==4367== Conditional jump or move depends on uninitialised value(s)
==4367==    at 0x51AB82D: _itoa_word (_itoa.c:179)
==4367==    by 0x51C76F4: __vfprintf_internal (vfprintf-internal.c:1687)
==4367==    by 0x51CA021: buffered_vfprintf (vfprintf-internal.c:2377)
==4367==    by 0x51C6EA3: __vfprintf_internal (vfprintf-internal.c:1346)
==4367==    by 0x527E022: __fprintf_chk (fprintf_chk.c:33)
==4367==    by 0x11B9CD: fprintf (stdio2.h:100)
==4367==    by 0x11B9CD: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:662)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Uninitialised value was created by a stack allocation
==4367==    at 0x11B860: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:596)
==4367==
Error: timestamp error for card 0 handle A1 (blk 0) ... expected 0x0000000000000000 but got 0x0000000003938724 (delta 60000036)
Info: all 0 card(s) started streaming
Info: sleeping for 3 seconds
Info: signaling to all 1 card(s) to stop streaming
Info: waiting for control thread for card 0
Info: stopping 0 Rx handle(s) on card 0 on next 1PPS pulse
==4367== Invalid read of size 2
==4367==    at 0x11BB31: __copy_m<short int volatile*, int*> (stl_algobase.h:340)
==4367==    by 0x11BB31: __copy_move_a<false, short int volatile*, int*> (stl_algobase.h:404)
==4367==    by 0x11BB31: __copy_move_a2<false, short int volatile*, int*> (stl_algobase.h:440)
==4367==    by 0x11BB31: copy<short int volatile*, int*> (stl_algobase.h:474)
==4367==    by 0x11BB31: _M_assign_aux<short int volatile*> (vector.tcc:321)
==4367==    by 0x11BB31: _M_assign_dispatch<short int volatile*> (stl_vector.h:1625)
==4367==    by 0x11BB31: assign<short int volatile*> (stl_vector.h:766)
==4367==    by 0x11BB31: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:674)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Address 0x7745000 is 0 bytes after a block of size 33,554,432 alloc'd
==4367==    at 0x483E0F0: memalign (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4367==    by 0x483E212: posix_memalign (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4367==    by 0x17A722: DmaInterfaceInit (in /home/red/recorder/sidekiq/a.out)
==4367==    by 0x3: ???
==4367==    by 0x1FFF00002F: ???
==4367==    by 0x3FFF: ???
==4367==    by 0x53560BF: ???
==4367==    by 0x7FF: ???
==4367==
==4367== Invalid read of size 2
==4367==    at 0x11BB20: __copy_m<short int volatile*, int*> (stl_algobase.h:342)
==4367==    by 0x11BB20: __copy_move_a<false, short int volatile*, int*> (stl_algobase.h:404)
==4367==    by 0x11BB20: __copy_move_a2<false, short int volatile*, int*> (stl_algobase.h:440)
==4367==    by 0x11BB20: copy<short int volatile*, int*> (stl_algobase.h:474)
==4367==    by 0x11BB20: _M_assign_aux<short int volatile*> (vector.tcc:321)
==4367==    by 0x11BB20: _M_assign_dispatch<short int volatile*> (stl_vector.h:1625)
==4367==    by 0x11BB20: assign<short int volatile*> (stl_vector.h:766)
==4367==    by 0x11BB20: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:674)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==  Address 0x7745002 is 2 bytes after a block of size 33,554,432 alloc'd
==4367==    at 0x483E0F0: memalign (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4367==    by 0x483E212: posix_memalign (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4367==    by 0x17A722: DmaInterfaceInit (in /home/red/recorder/sidekiq/a.out)
==4367==    by 0x3: ???
==4367==    by 0x1FFF00002F: ???
==4367==    by 0x3FFF: ???
==4367==    by 0x53560BF: ???
==4367==    by 0x7FF: ???
==4367==
==4367==
==4367== Process terminating with default action of signal 11 (SIGSEGV)
==4367==  Bad permissions for mapped region at address 0x7746000
==4367==    at 0x11BB31: __copy_m<short int volatile*, int*> (stl_algobase.h:340)
==4367==    by 0x11BB31: __copy_move_a<false, short int volatile*, int*> (stl_algobase.h:404)
==4367==    by 0x11BB31: __copy_move_a2<false, short int volatile*, int*> (stl_algobase.h:440)
==4367==    by 0x11BB31: copy<short int volatile*, int*> (stl_algobase.h:474)
==4367==    by 0x11BB31: _M_assign_aux<short int volatile*> (vector.tcc:321)
==4367==    by 0x11BB31: _M_assign_dispatch<short int volatile*> (stl_vector.h:1625)
==4367==    by 0x11BB31: assign<short int volatile*> (stl_vector.h:766)
==4367==    by 0x11BB31: sidekiq_class::receive_data(unsigned char, sidekiq_class::radio_config*) (testing_cpp.cpp:674)
==4367==    by 0x11BCF7: sidekiq_class::receive_run(void*) (testing_cpp.cpp:705)
==4367==    by 0x4DDB608: start_thread (pthread_create.c:477)
==4367==    by 0x526F102: clone (clone.S:95)
==4367==
==4367== HEAP SUMMARY:
==4367==     in use at exit: 33,633,366 bytes in 188 blocks
==4367==   total heap usage: 197 allocs, 9 frees, 33,839,742 bytes allocated
==4367==
==4367== Thread 1:
==4367== 288 bytes in 1 blocks are possibly lost in loss record 8 of 16
==4367==    at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4367==    by 0x40149CA: allocate_dtv (dl-tls.c:286)
==4367==    by 0x40149CA: _dl_allocate_tls (dl-tls.c:532)
==4367==    by 0x4DDC322: allocate_stack (allocatestack.c:622)
==4367==    by 0x4DDC322: pthread_create@@GLIBC_2.2.5 (pthread_create.c:660)
==4367==    by 0x11B58C: sidekiq_class::read_iq_on_pps(unsigned long, unsigned long) (testing_cpp.cpp:314)
==4367==    by 0x11AC73: main (testing_cpp.cpp:845)
==4367==
==4367== 288 bytes in 1 blocks are possibly lost in loss record 9 of 16
==4367==    at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==4367==    by 0x40149CA: allocate_dtv (dl-tls.c:286)
==4367==    by 0x40149CA: _dl_allocate_tls (dl-tls.c:532)
==4367==    by 0x4DDC322: allocate_stack (allocatestack.c:622)
==4367==    by 0x4DDC322: pthread_create@@GLIBC_2.2.5 (pthread_create.c:660)
==4367==    by 0x11B5A2: sidekiq_class::read_iq_on_pps(unsigned long, unsigned long) (testing_cpp.cpp:316)
==4367==    by 0x11AC73: main (testing_cpp.cpp:845)
==4367==
==4367== LEAK SUMMARY:
==4367==    definitely lost: 0 bytes in 0 blocks
==4367==    indirectly lost: 0 bytes in 0 blocks
==4367==      possibly lost: 576 bytes in 2 blocks
==4367==    still reachable: 33,632,790 bytes in 186 blocks
==4367==         suppressed: 0 bytes in 0 blocks
==4367== Reachable blocks (those to which a pointer was found) are not shown.
==4367== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==4367==
==4367== For lists of detected and suppressed errors, rerun with: -s
==4367== ERROR SUMMARY: 17000 errors from 14 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
Thread Thread
 
kyrlon profile image
kyrlon

The most puzzling part is that I get this error on one system but not the other:

No Problems:
cat /proc/version
Linux version 4.15.0-29-generic (buildd@lgw01-amd64-057) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #31-Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018

Problems(SegFault):
cat /proc/version
Linux version 5.4.0-42-generic (buildd@lgw01-amd64-038) (gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)) #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

You must remember that undefined behavior is exactly that. It may appear to work, and then not work elsewhere. When it doesn't work, anything could happen, including "making demons fly out your nose". So, I'd start by going through that Valgrind output, bit by bit, and fixing each problem in your code it highlights. (The error and location of that error in your code is on the last line of each traceback block in Valgrind.)

Once your //own// code runs Valgrind pure, we can tackle any remaining weirdness.

Thread Thread
 
kyrlon profile image
kyrlon

After furious placements of std::cout everywhere, I discovered that my function that my thread was calling was missing its return statement. Not sure how my first system was able to bypass that issue but it arised on my second system.

Thread Thread
 
codemouse92 profile image
Jason C. McDonald

So, it's resolved, then?

Thread Thread
 
kyrlon profile image
kyrlon

The issue yes. My curiosity, not so much lol. It shouldn't have compiled however that behavior I believe isactually allowed, it's grandfathered in from C. Since the function returns a variable and a return is not explicitly called then the first value in the stack frame is reinterpreted as the return type and returned instead

int foo(int bar) {
int blah= 8;
blah += 4;
int zayxxy = 0;
} //returns 12

So I believe that is the reason how my first system was able to execute without any problems. However, I do not fully understand how on the second system the function was never able to terminate the function but stay stuck in a while loop and result in a segfault.

Thread Thread
 
codemouse92 profile image
Jason C. McDonald • Edited

What you described, not returning a value from a non-void function, is actually undefined behavior in C. Therefore, once again, it is legal for the compiler to make demons fly out your nose. Anything can happen. There is no rhyme or reason.

Here's C99 on it — ISO/IEC 9899:1999, section 6.9.1 paragraph 12:

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

One system's compiler was able to figure it out anyway, and it worked, which is legal (because anything is). The other system's compiler was not, and it had a snit.

P.S. Thanks for asking! I learned something new today, namely that the above is undefined behavior.

Thread Thread
 
kyrlon profile image
kyrlon

Glad I could help! I have definitely learned alot from this experience as well!

Thread Thread
 
ac000 profile image
Andrew Clayton

OK, firstly, a small nitpick, you weren't getting a segfault (SIGSEGV) but a SIGABRT

As for failing to return a value from a non-void function, you should at the very least compile with -Wall, which would have caught that. e.g

/* n.c - no return from non-void function */

static int test(void)
{
}
$ gcc -c n.c
$

vs

$ gcc -Wall -c n.c
n.c: In function ‘test’:
n.c:5:1: warning: no return statement in function returning non-void [-Wreturn-type]
    5 | }
      | ^
At top level:
n.c:3:12: warning: ‘test’ defined but not used [-Wunused-function]
    3 | static int test(void)
      |            ^~~~
$

And of course we also get the second warning...

I always compile with at least '-Wall -Wextra'