Recent

Author Topic: Any ideas on this 'SIGSEV' error?  (Read 11316 times)

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Any ideas on this 'SIGSEV' error?
« on: March 29, 2011, 06:18:01 am »
I have a function that works fine but only once. When I try calling it again I get a SIGSEV error. I know it is something simple I just am not catching. Do I need to reinitialize something or free something? Thanks in advance.

CalcPHI2 is the function. Hangs during second call.

Quote
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls;

type
  TArray=array of double;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    PaintBox1: TPaintBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
  /////////////////////////////////////////////////////////////////////

  function calcPHI2(BaseN:double;count:integer;pos:boolean):Tarray;
  function calcPHIdbl(BaseN:double):tarray;

  procedure memodump1(str:string);
  procedure memodump(ar:tarray);

  procedure redrawArX(ar:tarray);
  /
var
  Form1: TForm1;

implementation
//////////////////////////////////////////////////////////////////
function calcPHI2(BaseN:double;count:integer;pos:boolean):Tarray;
var
  ar1,ar2,R_array:tarray;
  i,i2:integer;
  Base_i,maj,min :double ;
begin
ar1:=nil;
ar2:=nil;
R_array:=nil;

   base_i:=baseN;
   setlength(ar1,count);
   setlength(ar2,count);
   ar1[0]:=Base_i;
   ar2[count]:=0;
  for i:=0 to count-1 do
    begin
    ar1:=base_i*0.618;
    ar2:=baseN-(base_i-ar1);
    base_i:=ar1;
    memodump1(inttostr(round(ar1)));
    end;

  base_i:=baseN;

  for i:= 0 to count-1  do
    begin
    ar1:=base_i*0.618;
    ar2:=baseN-(base_i-ar1);
    base_i:=ar1;

   memodump1('---'+inttostr(round(ar2)));
    end;
  i2:=(count*2)-1;
  setlength(R_array,(count*2)-1);
  for i:=0 to Count-1 do
    begin
    R_Array:=ar1;
    R_Array[i2]:=ar2;
    i2:=i2-1;
    end;

   result:=R_Array;
 { memodump1('*****');
  memodump(ar1);
  memodump1('*****');
  memodump(ar2);
  memodump1('*****');
  memodump(R_array);}

end;

///////////////////////////////////////////////////
///////////////////////////////////////////////////

function calcPHIdbl(BaseN:double):tarray;
var
  arA,arB,r_array:tarray;
  i,i2,ic:integer;
  begin

  arA:=calcPHI2(BaseN*0.618,6,true);             ///// works fine for first call to calcphi2
  arB:=calcPhi2(BaseN-(baseN*0.618),6,true);     ///// fails here!!   (SIGSEV)

  ic:=length(ar1);
  setlength(r_array,length(ar1)+length(ar2));

  for i:= 0 to ic-1 do
    begin
    r_array:=arA;
    end;

   for i2:= (length(r_array)-1) downto ic do
    begin
    r_array[i2]:=(baseN*0.618)-arB[i2];
    end;
  result:=arA;
  end;

procedure memodump(ar:tarray);
var
  i:integer;
begin
//form1.memo1.clear;

For i:= 0 to length(ar)-1 do
  begin

  form1.memo1.Lines.add(floattostr(round(ar)));      ///rounded to integer

  end;
end;

procedure memodump1(str:string);
begin
form1.Memo1.lines.add(str);

end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  a:tarray;
begin
  a:=calcPHI2(300,4,true);////works fine but only 1 time
  memodump1('//////////');////2nd try returns 'SIGSEV' error
  redrawArX(a);

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  a:tarray;
begin
  a:=calcphidbl(300);
  redrawArX(a);
end;

procedure redrawArX(ar:tarray);
var
  i:integer;
begin
form1.paintbox1.canvas.clear;
form1.paintbox1.canvas.pen.Color:=clred;

For i:= 0 to length(ar)-1 do
  begin

  form1.paintbox1.canvas.moveto(round(ar),0);      ///rounded to integer
  form1.paintbox1.canvas.Lineto(round(ar),100);
  end;
end;

{$R *.lfm}

end.
           

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: Any ideas on this 'SIGSEV' error?
« Reply #1 on: March 29, 2011, 08:02:24 am »
Please disregard the obvious errors (like ar1 should be arA in a few spots). Also I see some funky things i did while trying to find the issue. Don't want to post another wall of code!

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Any ideas on this 'SIGSEV' error?
« Reply #2 on: March 29, 2011, 12:47:34 pm »
I think this:
Code: [Select]
ar1:=base_i*0.618;
should be this:
Code: [Select]
ar1[i]:=base_i*0.618;
when it is array.

Also this:
Code: [Select]
ar1:=nil;
is not necessary for arrays IMO.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Any ideas on this 'SIGSEV' error?
« Reply #3 on: March 29, 2011, 01:01:37 pm »
I replaced TPaintBox with TImage

I don't know if the code does what you want but at least it does not crash.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls;

type
  TArray=array of double;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Image1: TImage;
    Memo1: TMemo;
  //  PaintBox1: TPaintBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;
  /////////////////////////////////////////////////////////////////////

  function calcPHI2(BaseN:double;count:integer;pos:boolean):Tarray;
  function calcPHIdbl(BaseN:double):tarray;

  procedure memodump1(str:string);
  procedure memodump(ar:tarray);

  procedure redrawArX(ar:tarray);

var
  Form1: TForm1;

implementation
//////////////////////////////////////////////////////////////////
function calcPHI2(BaseN:double;count:integer;pos:boolean):Tarray;
var
  ar1,ar2,R_array:tarray;
  i,i2:integer;
  Base_i,maj,min :double ;
begin
{ar1:=nil;
ar2:=nil;
R_array:=nil;}

   base_i:=baseN;
   setlength(ar1,count);
   setlength(ar2,count);
   ar1[0]:=Base_i;
 //  ar2[count]:=0;  //this is wrong
  for i:=0 to count-1 do
    begin
    ar1[i]:=base_i*0.618;
    ar2[i]:=baseN-(base_i-ar1[i]);
    base_i:=ar1[i];
    memodump1(inttostr(round(ar1[i])));
    end;

  base_i:=baseN;

  for i:= 0 to count-1  do
    begin
    ar1[i]:=base_i*0.618;
    ar2[i]:=baseN-(base_i-ar1[i]);
    base_i:=ar1[i];

   memodump1('---'+inttostr(round(ar2[i])));
    end;
  i2:=(count*2)-1;
  setlength(R_array,(count*2)-1);
  for i:=0 to Count-1 do
    begin
    R_Array[i]:=ar1[i];
    R_Array[i2]:=ar2[i];
    i2:=i2-1;
    end;

   result:=R_Array;
 { memodump1('*****');
  memodump(ar1);
  memodump1('*****');
  memodump(ar2);
  memodump1('*****');
  memodump(R_array);}

end;

///////////////////////////////////////////////////
///////////////////////////////////////////////////

function calcPHIdbl(BaseN:double):tarray;
var
  arA,arB,r_array:tarray;
  i,i2,ic:integer;
  begin

  arA:=calcPHI2(BaseN*0.618,6,true);             ///// works fine for first call to calcphi2
  arB:=calcPhi2(BaseN-(baseN*0.618),6,true);     ///// fails here!!   (SIGSEV)

  ic:=length(arA);
  setlength(r_array,length(arA)+length(arB));

  for i:= 0 to ic-1 do
    begin
    r_array[i]:=arA[i];
    end;

   for i2:= (length(r_array)-1) downto ic do
    begin
    r_array[i2]:=(baseN*0.618)-arB[i2];
    end;
  result:=arA;
  end;

procedure memodump(ar:tarray);
var
  i:integer;
begin
//form1.memo1.clear;

For i:= 0 to length(ar)-1 do
  begin

  form1.memo1.Lines.add(floattostr(round(ar[i])));      ///rounded to integer

  end;
end;

procedure memodump1(str:string);
begin
form1.Memo1.lines.add(str);

end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
  a:tarray;
begin
  a:=calcPHI2(300,4,true);////works fine but only 1 time
  memodump1('//////////');////2nd try returns 'SIGSEV' error
  redrawArX(a);

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  a:tarray;
begin
  a:=calcphidbl(300);
  redrawArX(a);
end;

procedure redrawArX(ar:tarray);
var
  i:integer;
begin
form1.image1.canvas.clear;
form1.image1.canvas.pen.Color:=clred;

For i:= 0 to length(ar)-1 do
  begin

  form1.image1.canvas.moveto(round(ar[i]),0);      ///rounded to integer
  form1.image1.canvas.Lineto(round(ar[i]),100);
  end;
end;

{$R *.lfm}

end.         
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Any ideas on this 'SIGSEV' error?
« Reply #4 on: March 29, 2011, 01:12:06 pm »
Please disregard the obvious errors (like ar1 should be arA in a few spots)
Then how can we know what errors you actually have spotted yourself?
You can simply update the post with the correct(ed) code!

Code: Pascal  [Select][+][-]
  1. ar2[count]:=0;
I think there is no index 'count', only 'count-1'.
« Last Edit: March 29, 2011, 01:19:07 pm by eny »
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: Any ideas on this 'SIGSEV' error?
« Reply #5 on: March 29, 2011, 07:22:18 pm »
Sorry about that. I think i posted after throwing in the towel. I was swapping/changing things in desperation and posted an untested/compiled version,apparently.

Is there an 'edit post' button somewhere here? Can't find it.Maybe my eyes are going on me,haha. Maybe a break is what I need.

Thanks Blazen. I think I will try your approach. Gonna try BGRAbitmap. I hoped it was some simple,obvious mistake I was missing.

It is strange behavior seeing as how I had it working just fine in another project (until it started acting up. Buttons stopped appearing so I started a fresh project). I mean, it is really basic stuff.

If it's not an obvious 'newbie' mistake, I will just press on with it.

Thanks for your help, fellas.


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12307
  • Debugger - SynEdit - and more
    • wiki
Re: Any ideas on this 'SIGSEV' error?
« Reply #6 on: March 29, 2011, 08:30:14 pm »
Do you run your app in the debugger? With debug symbols and all?

Because then the debugger should catch the error, you should be able to open the stack window, and find the exact line of the error.

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: Any ideas on this 'SIGSEV' error?
« Reply #7 on: March 29, 2011, 10:11:46 pm »
Do you run your app in the debugger? With debug symbols and all?

Because then the debugger should catch the error, you should be able to open the stack window, and find the exact line of the error.

I did...somewhat...but you raise a good point. I need to get more familiar with the debugger than I am.

Still can't figure why it runs fine once but hangs at the second call. It is as simple as it gets, I was hoping. Just a number divided up and shuffled up and passed back as two arrays....So if I found it in debugger it would probably be something 'out of my league', I would imagine.HAHA

I will run it w/debug in case there is an issue that needs reporting.

I am using FPC 2.5.1 btw.

Thanks


Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Any ideas on this 'SIGSEV' error?
« Reply #8 on: March 29, 2011, 10:28:09 pm »
Quote
Still can't figure why it runs fine once but hangs at the second call.

Did you try the code I posted above? You can press buttons how many times you want, it does not crash ...

I think your original code crashed because of bad indexing of dynamic arrays.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: Any ideas on this 'SIGSEV' error?
« Reply #9 on: March 29, 2011, 11:03:37 pm »
Quote
Still can't figure why it runs fine once but hangs at the second call.

Did you try the code I posted above? You can press buttons how many times you want, it does not crash ...

I think your original code crashed because of bad indexing of dynamic arrays.

I added a Timage and swapped it out for the Tpaintbox. Same issue.Was that all you did or was there something you did different w/the dynamic arrays?

I also am trying it with BGRAbitmap. Having problems with it too. This won't compile...

 Error: Incompatible type for arg no. 4: Got "PBGRAPixel", expected "TBGRAPixel"


Quote
procedure redrawArX(ar:tarray);
var
  i:integer;
  bmp,bmp2: TBGRABitmap;
  p: PBGRAPixel;
begin

bmp2 := TBGRABitmap.Create(100,100,BGRABlack);
p:=bmp2.data;

For i:= 0 to length(ar)-1 do
  begin
   //creates a 100x100 pixels image with black background

  bmp.SetVertLine(0, 0, round(Ar),p);

  bmp.Draw(form1.image1.Canvas, 0, 0, True); // draw the bitmap in opaque mode (faster)
  end;

Should I just install FPC 2.4 maybe?

Thanks again.


CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: Any ideas on this 'SIGSEV' error?
« Reply #10 on: March 29, 2011, 11:08:38 pm »
Quote
procedure redrawArX(ar:tarray);
var
  i:integer;
  bmp,bmp2: TBGRABitmap;
  p: PBGRAPixel;
begin

bmp2 := TBGRABitmap.Create(100,100,BGRABlack);
p:=bmp2.data;

For i:= 0 to length(ar)-1 do
  begin
   //creates a 100x100 pixels image with black background

  bmp.SetVertLine(0, 0, round(Ar),p);

  bmp.Draw(form1.image1.Canvas, 0, 0, True); // draw the bitmap in opaque mode (faster)
  end;

[/quote]

Crap! did it again.

Disregard the "var p: pBGRAbitmap". I posted the wrong code. "var p:TBRGBbitmap" get the same response.


Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Any ideas on this 'SIGSEV' error?
« Reply #11 on: March 29, 2011, 11:11:52 pm »
Quote
Was that all you did or was there something you did different w/the dynamic arrays?

I repaired dyn. array indexing on at least twenty places. See the code above, that why I posted it.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: Any ideas on this 'SIGSEV' error?
« Reply #12 on: March 29, 2011, 11:32:57 pm »
Quote
Was that all you did or was there something you did different w/the dynamic arrays?

I repaired dyn. array indexing on at least twenty places. See the code above, that why I posted it.

I should have just pasted it and would have been good to go, in other words? Duh. My apologies. I know it is some simple mistake of mine going on and I will look at what you did closer...Going to have to because i still don't see any other changes for the life of me,HAHA.

Thanks for help

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: Any ideas on this 'SIGSEV' error?
« Reply #13 on: March 30, 2011, 01:43:28 am »
Wow! How did I miss those glaring errors? I am surprised it even compiled....Works fine now(even with Tpaintbox). Tricked me by compiling like that I guess.

I am still having trouble with the BGRAbitmap though if you have any ideas. The code is strait from the tutorials, not mine. Seems to be getting a pointer instead of actual tbgra. Is there an alteranative calling method or workaround?

Thanks again.

CaptBill

  • Sr. Member
  • ****
  • Posts: 435
Re: Any ideas on this 'SIGSEV' error?
« Reply #14 on: March 30, 2011, 05:43:58 am »
As it turns out, there is a problem after all. It doesn't crash but is returning different values on the second and subsequent calls for some reason. I installed the new Lazarus 2.3 and the problem is solved.

Thanks

 

TinyPortal © 2005-2018