Recent

Author Topic: [Solved]Lazarus ProgrssBar throws error  (Read 7924 times)

Littellittel

  • New member
  • *
  • Posts: 8
[Solved]Lazarus ProgrssBar throws error
« on: April 08, 2016, 05:44:35 pm »
Hello,

I`m not new to programming but to lazarus as a programming software. I wanted to use some Progressbars to show the users their Progress, it worked for some time but then, with no reason it stoped working. As i start the Programm it immediately throws this error: Exception-Klass >>External:SIGSEGV<< and then it puts me to include\progressbar.inc to Line 101. It does this with every command that asks for some variables such as progressbar.max, progressbar.min etc.

thank in advance
« Last Edit: April 10, 2016, 01:27:19 pm by Littellittel »

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: Lazarus ProgrssBar throws error
« Reply #1 on: April 08, 2016, 05:51:11 pm »
Which Lazarus version?
Which fpc version?
Can you reproduce the crash in a simple application (1 ProgressBar on 1 Form, no other controls)?
If so, then please attach such a sample projected (zipped sources only).

Bart

Littellittel

  • New member
  • *
  • Posts: 8
Re: Lazarus ProgrssBar throws error
« Reply #2 on: April 08, 2016, 06:33:17 pm »
thanks for the quick answere.
I use Lazarus version 1.6 and fpc version 3.0.0. Also i could not manage to reproduce the error. I will try copying my projekt over to new one, heard that that sometimes helps.

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: Lazarus ProgrssBar throws error
« Reply #3 on: April 08, 2016, 06:38:39 pm »
Run your program from within the debugger and see where it crashes.
Produce a backtrace and see if that leads you anywhere.

Bart

Littellittel

  • New member
  • *
  • Posts: 8
Re: Lazarus ProgrssBar throws error
« Reply #4 on: April 08, 2016, 08:17:51 pm »
Ok,

I have some new Information. It happens because i don´t call the Progressbar directly, i define a Variable with the Type of an TProgressbar (because i want that one function can update multiple ones depending on a Variable) and then write something like this:"ProgressBar:=Progressbar1" and then I just say "ProgressBar.position:=ProgressBar.Max". If i call "ProgressBar1.position:=Progressbar1.Max" it works.

So my question is now, did i understand something wrong about the Variable thing with Variable:=TProgressBar (I mean it worked for some time)? And if yes is there another easy way to achive that the function can work with one Variable instead of a case to check wich Progressbar should be updatet?

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: Lazarus ProgrssBar throws error
« Reply #5 on: April 08, 2016, 10:55:07 pm »
Show us the code.
(Minimal test case)

Bart

Littellittel

  • New member
  • *
  • Posts: 8
Re: Lazarus ProgrssBar throws error
« Reply #6 on: April 09, 2016, 05:25:16 pm »
I tried to do that but i can`t. It sems like it is a Problem with my Project or something. In other Project the error doesn`t appear but the code i have throws this error, also i noticed just now that lazarus tells me that the local Variable ProgressBar is assigned but never used.

I assigned it like this:

Code: Pascal  [Select][+][-]
  1. ProgressBar:TProgressBar;
  2. ProgressBar:=ProgressBar1;
later i use it like this:
Code: Pascal  [Select][+][-]
  1. ProgressBar.Position:=10;
and that throws the error

but
Code: Pascal  [Select][+][-]
  1. ProgressBar1.Position:=10;
works

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Lazarus ProgrssBar throws error
« Reply #7 on: April 09, 2016, 05:39:39 pm »
ProgressBar may be an uninitialized alias identifier hiding somewhere.

Best to remove all references to ProgressBar (declaration(s) and uses) and use only the valid reference ProgressBar1,
or at the least protect your use of ProgressBar with a
Code: Pascal  [Select][+][-]
  1. if (ProgressBar <> nil) then
  2.   ProgressBar.Position:= ...
  3.  

Or you could use an assertion, or a debugln() or showMessage() or similar to alert you to an uninitialized reference.
« Last Edit: April 09, 2016, 05:43:14 pm by howardpc »

Littellittel

  • New member
  • *
  • Posts: 8
Re: Lazarus ProgrssBar throws error
« Reply #8 on: April 09, 2016, 06:06:54 pm »
sadly it isn't i made a misstage I sayed earlyer that lazarus says me that the variable Progressbar is not used but that is only because I commendet the code that causes the error (every line where ProgressBar was used) so it is of course used. So Your example doesn't help, bacause ProgressBar is always <> Nil. And writing ProgressBar1.Position and there for ProgressBar2.Position... is not really an option because that would make my function at least like 30 lines bigger.

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: Lazarus ProgrssBar throws error
« Reply #9 on: April 09, 2016, 06:36:57 pm »
Last time: show us the entire relevant code, preferrably in a minimal test case.
Otherwise this becomes a ÿou say yes", "I say no" game.
There is plenty of code like your example around in LCL and this does not crash, so me thinks the error is on your side.

E.g. something like this works without any problem on a form with 4 progressbars:
Code: [Select]
type

  TPBArr = Array[0..3] of TProgressBar;
  TForm1 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    ProgressBar2: TProgressBar;
    ProgressBar3: TProgressBar;
    ProgressBar4: TProgressBar;
    RG: TRadioGroup;
  private
    PBArr: TPBArr;
  public
  end;

implementation

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 PBArr[0] := ProgressBar1;
 PBArr[1] := ProgressBar2;
 PBArr[2] := ProgressBar3;
 PBArr[3] := ProgressBar4;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  PB: TProgressBar;
  Idx: Integer;
begin
  Idx := RG.ItemIndex;
  if (Idx > -1) and (Idx <= High(TPBArr)) then
    PB := PBarr[Idx]
  else
    PB := nil;
  if not Assigned(PB) then Exit;
  if (PB.Position = PB.Max) then
    PB.Position := (PB.Max - PB.Min) div 2
  else
    PB.Position := PB.Max;
end;

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Lazarus ProgrssBar throws error
« Reply #10 on: April 09, 2016, 08:56:33 pm »
So Bart has given you working compilable code that uses a variable (named PB) to reference a variety of different progress bars successfully.

SIGSEGV errors are most usually the result of trying to access an incorrectly instantiated (or nil) object. So notice how Bart writes
Code: Pascal  [Select][+][-]
  1. if { condition for validity satisfied} then
  2.   PB:=xx // some valid assignment
  3. else PB:=nil;
  4. if not Assigned(PB) then
  5.   Exit;
  6.  
This is the sort of approach you need.

BTW in his example you can dispense with the button and button click and simply insert the buttonClick code into the radiogroup's OnSelectionChange event.

Littellittel

  • New member
  • *
  • Posts: 8
Re: Lazarus ProgrssBar throws error
« Reply #11 on: April 09, 2016, 10:06:56 pm »
Ok sorry, i wanted to prevent that because i couldn`t creat a minimal test case but i will just post all code that has something to do with the ProgressBar:

Code: Pascal  [Select][+][-]
  1. //in FormCreat
  2. Variables[1][2]:=100; //all Variables are in an array to keep the programm easy to change
  3. ProgressBar1.Min:=0;
  4. ProgressBar1.Max:=trunc(Variables[1][2]);
  5.  
  6. //in the timer procedure (not the actuall timer it's just called like that)
  7. var
  8.   ProgressBar: TProgressBar;
  9. begin
  10.   ProgressBar:=ProgressBar1; //ProgressBar1 is the name of the ProgressBar and this name makes sense in my case also it isn't always ProgressBar1 it can ProgressBar1-10 but that shouldn't be a Problem
  11.  
  12. ProgressBar.Position:=ProgressBar.Max; //and this makes the error
  13. end;
  14.  

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1269
Re: Lazarus ProgrssBar throws error
« Reply #12 on: April 09, 2016, 10:18:42 pm »
Coding style aside, this code shouldn't AV, unless you're doing something wrong. 

Can you breakpoint on Progressbar:=ProgressBar1; and determine if progressbar1 is actually assigned at that point?   I'm guessing not...

Quote
i will just post all code that has something to do with the ProgressBar:

Please do.  You can Publish your project from lazarus, zip it, and if it's small enough, post here as an attachment.
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

Bart

  • Hero Member
  • *****
  • Posts: 5706
    • Bart en Mariska's Webstek
Re: Lazarus ProgrssBar throws error
« Reply #13 on: April 09, 2016, 11:51:29 pm »
Code: Pascal  [Select][+][-]
  1. //in FormCreat
  2. Variables[1][2]:=100; //all Variables are in an array to keep the programm easy to change
  3. ProgressBar1.Min:=0;
  4. ProgressBar1.Max:=trunc(Variables[1][2]);
  5.  
  6. //in the timer procedure (not the actuall timer it's just called like that)
  7. var
  8.   ProgressBar: TProgressBar;
  9. begin
  10.   ProgressBar:=ProgressBar1; //ProgressBar1 is the name of the ProgressBar and this name makes sense in my case also it isn't always ProgressBar1 it can ProgressBar1-10 but that shouldn't be a Problem
  11.  
  12. ProgressBar.Position:=ProgressBar.Max; //and this makes the error
  13. end;
  14.  

Again you fail to give us the relevant code.
I give up.

Bart

Littellittel

  • New member
  • *
  • Posts: 8
Re: Lazarus ProgrssBar throws error
« Reply #14 on: April 10, 2016, 10:27:31 am »
OK, i hoped that the code that has to do with the ProgressBar is the relevant code but here is my hole project. It will be a little game similar to cookie clicker, so you know what you are up to  :D.

 

TinyPortal © 2005-2018