Recent

Author Topic: Segfault at the very end of the program  (Read 30890 times)

TCH

  • Full Member
  • ***
  • Posts: 200
Segfault at the very end of the program
« on: October 30, 2017, 11:45:50 pm »
My program suddenly started to throw SIGSEGV when i close my program. I tried to debug it, i had put a breakpoint at the beginning of the main form's Close action and then with F7, i stepped forward. I reached the "end." at the end of the program and then i got the segfault. If i don't debug, just quit, the debugger will stands in RaiseGDBException at line 902 of LCLProc.

How can i track where the segfault happens and how can i track from where my program jumped into RaiseGDBException?

And during stepping and then removing the breakpoints and continuing it throwed a SIGFPE. Maybe it's not my program at all, but it's one of the external shared objects, the program uses?

I tried to do strace externally:
Code: [Select]
[FORMS.PP] ExceptionOccurred
  Sender=EAccessViolation
  Exception=Access violation
  Stack trace:
  $000000000049973D
  $0000000000430640
  $0000000000560D84 line 1576 of include/canvas.inc
  $0000000000556DC8 line 83 of include/bitmapcanvas.inc
  $0000000000430640
  $00000000005558E8 line 57 of include/custombitmap.inc
  $0000000000430640
  $000000000069F3FD line 32 of include/customimage.inc
  $000000000049C21D
  $0000000000639269 line 4857 of include/control.inc
  $0000000000627B98 line 6579 of include/wincontrol.inc
  $000000000063B97F line 54 of include/customcontrol.inc
  $000000000044881F line 338 of include/scrollingwincontrol.inc
  $00000000004499A1 line 208 of include/customform.inc
  $0000000000430640
  $0000000000445AF6 line 1850 of forms.pp
TApplication.HandleException Access violation
  Stack trace:
  $000000000049973D
  $0000000000430640
  $0000000000560D84 line 1576 of include/canvas.inc
  $0000000000556DC8 line 83 of include/bitmapcanvas.inc
  $0000000000430640
  $00000000005558E8 line 57 of include/custombitmap.inc
  $0000000000430640
  $000000000069F3FD line 32 of include/customimage.inc
  $000000000049C21D
  $0000000000639269 line 4857 of include/control.inc
  $0000000000627B98 line 6579 of include/wincontrol.inc
  $000000000063B97F line 54 of include/customcontrol.inc
  $000000000044881F line 338 of include/scrollingwincontrol.inc
  $00000000004499A1 line 208 of include/customform.inc
  $0000000000430640
  $0000000000445AF6 line 1850 of forms.pp
Exception at 000000000049973D: EAccessViolation:
Access violation.

And i tried an older binary of my program which did not do this segfault thing, but now that does it too, so if the error is in my code, then it was there that time.
It only does this, since my last apt-get update && apt-get upgrade. (Debian 8, AMD64) That's why i ask, if it can be some of the external libs and if yes, how can i tell.
« Last Edit: November 07, 2017, 06:36:54 pm by TCH »

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: Segfault at the very end of the program
« Reply #1 on: October 31, 2017, 12:18:52 am »
Start you program but don't do anything, then just terminate it..

if it completes ok, then you need to do different steps in your program to reproduce it.

You can also place a break point in the main file at different steps of the Application to see
if it makes it at the base of the Main unit, if it does and faults after that, then look in the
finalization sections of all units you have made. place break points at the start of each.

The only true wisdom is knowing you know nothing

balazsszekely

  • Guest

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Segfault at the very end of the program
« Reply #3 on: October 31, 2017, 08:48:52 am »
@jamie: If i terminate it immediately, then it works of course. It's something in the depths of my program, but before the last system update it did not do it...

The segfault happens after the "end."

@GetMem:
Code: [Select]
root@Csabi:/media/ROOTDIR/lw/ytfe# gdb ./ytfe
GNU gdb (Debian 7.7.1+dfsg-5) 7.7.1
Copyright (C) 2014 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 ./ytfe...done.
(gdb) run
Starting program: /media/ROOTDIR/lw/ytfe/ytfe
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff09a0700 (LWP 24940)]
[New Thread 0x7ffff059f700 (LWP 24941)]
[New Thread 0x7fffebfff700 (LWP 24942)]
[New Thread 0x7fffebbfe700 (LWP 24943)]
[New Thread 0x7fffeb7fd700 (LWP 24944)]
[New Thread 0x7fffeb3bc700 (LWP 24945)]
[New Thread 0x7fffeaf09700 (LWP 24946)]
[New Thread 0x7fffeaa56700 (LWP 24947)]
[New Thread 0x7fffea655700 (LWP 24948)]
[New Thread 0x7fffea070700 (LWP 24949)]
[Thread 0x7fffebfff700 (LWP 24942) exited]
[Thread 0x7fffeaf09700 (LWP 24946) exited]
[Thread 0x7fffebbfe700 (LWP 24943) exited]
[Thread 0x7fffeb7fd700 (LWP 24944) exited]
[Thread 0x7fffeaa56700 (LWP 24947) exited]
[Thread 0x7fffeb3bc700 (LWP 24945) exited]
[Thread 0x7fffea655700 (LWP 24948) exited]
[Thread 0x7fffea070700 (LWP 24949) exited]
[Thread 0x7ffff09a0700 (LWP 24940) exited]
[Thread 0x7ffff059f700 (LWP 24941) exited]

Program received signal SIGSEGV, Segmentation fault.
0x000000000049976d in CLASSES$_$TPERSISTENT_$__$$_FPONOTIFYOBSERVERS$TOBJECT$TFPOBSERVEDOPERATION$POINTER ()
(gdb) bt
#0  0x000000000049976d in CLASSES$_$TPERSISTENT_$__$$_FPONOTIFYOBSERVERS$TOBJECT$TFPOBSERVEDOPERATION$POINTER ()
#1  0x0000000000000000 in ?? ()
(gdb) quit
A debugging session is active.

        Inferior 1 [process 24936] will be killed.

Quit anyway? (y or n) y
root@Csabi:/media/ROOTDIR/lw/ytfe#
Do you see any interesting or suspicious?

balazsszekely

  • Guest
Re: Segfault at the very end of the program
« Reply #4 on: October 31, 2017, 09:08:33 am »
Do you use threads in your application? It looks like one or more threads are still running. Just before you close the application, you should signal each thread that is time to quit, then wait until they are terminated.
It also can be a gdb issue. What if you run your application outside the IDE(no debugging)?

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Segfault at the very end of the program
« Reply #5 on: October 31, 2017, 09:47:24 am »
Yes, i use threads, but i stop all of them properly when exiting. Also, the strace told me, that the problem roots at the line 1850 of forms.pp which is
Code: Pascal  [Select][+][-]
  1. procedure BeforeFinalization;
  2. // This is our ExitProc handler.
  3. begin
  4.   Application.DoBeforeFinalization; // <= line 1850
  5. end;
If i try to run it from console, then the result is
Code: [Select]
root@Csabi:/media/ROOTDIR/lw/ytfe# ./ytfe
[FORMS.PP] ExceptionOccurred
  Sender=EAccessViolation
  Exception=Access violation
  Stack trace:
  $000000000049976D
  $0000000000430640
  $0000000000560DB4 line 1576 of include/canvas.inc
  $0000000000556DF8 line 83 of include/bitmapcanvas.inc
  $0000000000430640
  $0000000000555918 line 57 of include/custombitmap.inc
  $0000000000430640
  $000000000069F42D line 32 of include/customimage.inc
  $000000000049C24D
  $0000000000639299 line 4857 of include/control.inc
  $0000000000627BC8 line 6579 of include/wincontrol.inc
  $000000000063B9AF line 54 of include/customcontrol.inc
  $000000000044881F line 338 of include/scrollingwincontrol.inc
  $00000000004499A1 line 208 of include/customform.inc
  $0000000000430640
  $0000000000445AF6 line 1850 of forms.pp
TApplication.HandleException Access violation
  Stack trace:
  $000000000049976D
  $0000000000430640
  $0000000000560DB4 line 1576 of include/canvas.inc
  $0000000000556DF8 line 83 of include/bitmapcanvas.inc
  $0000000000430640
  $0000000000555918 line 57 of include/custombitmap.inc
  $0000000000430640
  $000000000069F42D line 32 of include/customimage.inc
  $000000000049C24D
  $0000000000639299 line 4857 of include/control.inc
  $0000000000627BC8 line 6579 of include/wincontrol.inc
  $000000000063B9AF line 54 of include/customcontrol.inc
  $000000000044881F line 338 of include/scrollingwincontrol.inc
  $00000000004499A1 line 208 of include/customform.inc
  $0000000000430640
  $0000000000445AF6 line 1850 of forms.pp
Exception at 000000000049976D: EAccessViolation:
Access violation.

I have a backup partition of my system which was updated one or two weeks ago, i booted it up and tried it, the result is the same. An older binary also do the same. It must were there all the time, i just did not noticed. Altough i don't really understand, that Lazarus why did not thrown these exceptions before...

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Segfault at the very end of the program
« Reply #6 on: October 31, 2017, 09:49:58 am »
I think the problem might be in my custom tab handler. Could you look it please? You're more experienced, maybe i implemented something wrong.

http://oscomp.hu/depot/buttontablist.pas
« Last Edit: October 31, 2017, 04:59:55 pm by TCH »

Mimmo

  • New Member
  • *
  • Posts: 21
Re: Segfault at the very end of the program
« Reply #7 on: October 31, 2017, 06:35:39 pm »
Hi,
maybe the problem is that if you are filling the FToogleBoxes and FButtons arrays with instances created with TToggleBox.Create(FOwner) and TButton.Create(FOwner), then you don't need to free them in the TButtonTabList.Destroy destructor method. They will be automatically deallocated when FOwner will be destroyed.
Domenico

mai

  • Full Member
  • ***
  • Posts: 133
  • truther
Re: Segfault at the very end of the program
« Reply #8 on: October 31, 2017, 07:24:28 pm »
unless I changed project settings I always got rt err 204 . fp would compile fine, but laz would not.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Segfault at the very end of the program
« Reply #9 on: November 01, 2017, 05:27:02 am »
Hi,
maybe the problem is that if you are filling the FToogleBoxes and FButtons arrays with instances created with TToggleBox.Create(FOwner) and TButton.Create(FOwner), then you don't need to free them in the TButtonTabList.Destroy destructor method. They will be automatically deallocated when FOwner will be destroyed.
Domenico
Rather than "don't need", you MUST NOT free them manually. Because the owner will just free them by calling .Free, hence they still point to the address despite the address is no longer valid, triggering error 204: Invalid pointer operation because .Free called on invalid address.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Segfault at the very end of the program
« Reply #10 on: November 01, 2017, 06:09:42 am »
Hi,
maybe the problem is that if you are filling the FToogleBoxes and FButtons arrays with instances created with TToggleBox.Create(FOwner) and TButton.Create(FOwner), then you don't need to free them in the TButtonTabList.Destroy destructor method. They will be automatically deallocated when FOwner will be destroyed.
Domenico
Rather than "don't need", you MUST NOT free them manually. Because the owner will just free them by calling .Free, hence they still point to the address despite the address is no longer valid, triggering error 204: Invalid pointer operation because .Free called on invalid address.
No. Owners are informed when owned components are freed and remove them from the internal list. the only way that this makes senese is if the control frees them after the owner where, as far as I can see, no handler is registered.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Segfault at the very end of the program
« Reply #11 on: November 01, 2017, 08:38:53 am »
I've removed the .Free calls in the destructor, but i still got a segfault when i close the main form.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Segfault at the very end of the program
« Reply #12 on: November 01, 2017, 09:24:31 am »
I downloaded and tested your TButtonTabList with Lazarus 1.8.0RC5 Linux64 Gtk2 with simple code:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   AButtonTabList := TButtonTabList.Create(Self);
  4.   AButtonTabList.Top := 10;
  5.   AButtonTabList.Left := 10;
  6.   AButtonTabList.Width := 100;
  7.   AButtonTabList.Enabled := True;
  8.   AButtonTabList.Free;
  9. end;

I cannot reproduce your error, can you provide simple code that can generate that error?

I think the bug is not inside that unit. If I'm having this issue I will try to remove some parts of the code, test it and repeat until I find the code that causes the error. Of course you have make backup of the code before you start.

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Segfault at the very end of the program
« Reply #13 on: November 01, 2017, 10:14:36 am »
Sorry, i cannot give any specific code, since i have no idea which part of the program causes the segfault. The segfault occurs when the program reach the last instruction in the entire program: "end."

Still, i think, you're right and it's not this unit. I have isolated two lines of code, which is if it's not uncommented, the program segfaults at exit. They are in two different subrutins, but both are in a cycle which fills twenty Memo and Image with data. But neither of them actually does anything wrong. They are working perfectly all along, but if they are not uncommented, the program crashes when i exit.

They are
Code: Pascal  [Select][+][-]
  1.      for i := 0 to tab_data[tabs.TabIndex].entrycount - 1 do
  2.      begin
  3.           infoimg[i].Picture.Clear;
  4.           infotx[i].Clear;
  5.           infotx[i].Lines.Add(decode_html_codes(tab_data[tabs.TabIndex].entries[i].title));
  6.           infotx[i].Lines.Add('');
  7.           if (tab_data[tabs.TabIndex].entries[i].playlist) then
  8.           begin
  9.                infotx[i].Lines.Add(decode_html_codes(tab_data[tabs.TabIndex].entries[i].user) + ' - ' + Trim(tab_data[tabs.TabIndex].entries[i].count));
  10.           end
  11.           else
  12.           begin
  13.                pre := tab_data[tabs.TabIndex].entries[i].length + ' | ' + decode_html_codes(tab_data[tabs.TabIndex].entries[i].user);
  14.                post := ' :: ' + tab_data[tabs.TabIndex].entries[i].views;
  15.                if (tab_data[tabs.TabIndex].is_video) then
  16.                begin
  17.                     infotx[i].Lines.Add(pre + post);
  18.                end
  19.                else
  20.                begin
  21.                     //infotx[i].Lines.Add(pre + ' - ' + tab_data[tabs.TabIndex].entries[i].upltime + post);
  22.                     infotx[i].Lines.Add('');
  23.                     infotx[i].Lines.Add(decode_html_codes(tab_data[tabs.TabIndex].entries[i].description));
  24.                end;
  25.           end;
  26.           infotx[i].SelStart := 0;
  27.           infotx[i].SelLength := 0;
  28.      end;
and
Code: Pascal  [Select][+][-]
  1.       for i := tab_data[tabs.TabIndex].scroll_pos to last_video do
  2.       begin
  3.            infotx[i].Top := ScrollBar1.Top + entry * space;
  4.            infoimg[i].Top := infotx[i].Top;
  5.            infoimg[i].Width := iw;
  6.            infoimg[i].Height := ih;
  7.            infoimg[i].Left := Form1.Width - (ScrollBar1.Width + iw + 2);
  8.            infotx[i].Width := infoimg[i].Left - 8;
  9.            infotx[i].Height := ih;
  10.            //infoimg[i].Visible := True;
  11.            infotx[i].Visible := True;
  12.            Inc(entry);
  13.       end;


 The array of Memos and Images are created inside the FormCreate method like this:
Code: Pascal  [Select][+][-]
  1.      for i := 0 to 19 do
  2.      begin
  3.           infotx[i] := TMemo.Create(Form1);
  4.           infotx[i].Parent := Form1;
  5.           infotx[i].Visible := False;
  6.           infotx[i].Color := clForm;
  7.           infotx[i].Height := 138;
  8.           infotx[i].Left := 4;
  9.           infotx[i].WordWrap := True;
  10.           infotx[i].ReadOnly := True;
  11.           infotx[i].ScrollBars := ssNone;
  12.           infoimg[i] := TImage.Create(Form1);
  13.           infoimg[i].Parent := Form1;
  14.           infoimg[i].Visible := False;
  15.           infoimg[i].Cursor := crHandPoint;
  16.           infoimg[i].OnMouseDown := @infoimg_mousedown;
  17.           infoimg[i].OnMouseUp := @infoimg_mouseup;
  18.           infoimg[i].Stretch := true;
  19.      end;
I did not call .Free on them anywhere.
« Last Edit: November 01, 2017, 10:16:47 am by TCH »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Segfault at the very end of the program
« Reply #14 on: November 01, 2017, 10:37:25 am »
Well there may be bugs elsewhere in your program, but certainly TButtonTablist does not handle creation and destruction of subcomponents correctly, especially the Synchronise method.
Can you say what functionality you want from TButtonTablist? Perhaps we can help redesign it to be more robust.
What is your code that creates the buttonlist and sets its properties, and what methods/properties of the list do you use?

 

TinyPortal © 2005-2018