Recent

Author Topic: [SOLVED] something is funny between 1.2 and 1.4 Lazarus  (Read 15797 times)

userx-bw

  • Full Member
  • ***
  • Posts: 178
[SOLVED] something is funny between 1.2 and 1.4 Lazarus
« on: April 24, 2015, 09:14:08 pm »
Ok this is what is going on now that I finally got both versions of Lazarus installed so I could check this.

when running my program compiled off of Var 1.4.0 I get an out of bounds error.

when running the very same code compiled off of ver 1.2.4 it works with out a hitch.

version 1.4 it just stopped working as it should. I get this error instead.

Code: [Select]
List index (0) out of bounds

press OK to ignore and risk data corruption
Press Cancel to kill the program

yet when I run the very idental program in ver 1.2 I do not get this error.

at first I thought it was a change in the FPC but both versions of Lazarus are using the same version FPC .  fpc_2.6.4-150228_amd64

therefore it tells me that it is not a change within a function call that I am using with the Free Pascal code. RIGHT?

this is the code that checks and if not than creates an TstringList then sets current directory then calls to fill that list with files.

Code: [Select]
procedure SetDir;
begin
       if not ( Form1.ImageList = nil ) then
     begin
      Form1.ImageList.Free;
      Form1.ImageList := TStringList.Create;
      Form1.ImageCount:=0;
      end;
    try
       if (SelectDirectory(Form1.CurrentDir, [sdPrompt], 0 ) = true)
        then
           begin
            SetCurrentDir(Form1.CurrentDir);
            FillImageList;
           end;
       except
             on E: Exception do
             ShowMessage('Exception! '+E.Message);
       end;
end;   

this is the code where I fill the list

Code: [Select]
procedure FillImageList;
begin                                                                          //no sub directory -
  Form1.ImageList := FindAllFiles(ExtractFilePath(Form1.ImageFile), '*.jpg; *.png', false);
  Form1.ImageList.Sort;
end; 

and this is the code that is now throwing this error. in the debugger - where commented it is tossing the error when it looks for the first image file I get an " instead of a file name to load using 1.4

Code: [Select]
procedure TForm1.CycleImages;
begin

  if (ImageList = nil)
  then
    begin
      MessageDlg('No Directory is Set', mtWarning, [mbOK],0);
      functions1.SetDir;
    end;

 try     // Right here it is tossing the error when it looks for the first image file I get an " instead of a file name using 1.4
  if (CompareFileExt(ImageList.Strings[Form1.ImageCount], 'jpg') = 0 ) or
     (CompareFileExt(ImageList.Strings[Form1.ImageCount], 'png') = 0 )
  then
      begin
            if (ImageCount >= ImageList.Count-1)
            then
              ImageCount := 0
            else
              ImageCount := ImageCount + 1;



      if FileExists(ImageList.Strings[ImageCount]) = true then
          begin
          Form1.ImageFile:=Form1.ImageList.Strings[ImageCount];
          Image.Picture.LoadFromFile(ImageFile);

          Form1.StatusBar1.SimpleText :=
          ExtractFileName(ImageList.Strings[ImageCount]) +' ' +' Total ' +
          IntToStr(Form1.ImageList.Count);
          Form1.Caption:='Image# ' + IntToStr(Form1.ImageCount)+'::'+
          IntToStr(Form1.ImageList.Count)+' Total';

          functions1.SetImageBackground;

          //Setbackground1.SetBackGroundImage;
          //Form1.Image.Refresh;
          //Form1.Image.Repaint;

      end;

      end;

  except on
  E: Exception do
  begin
    ShowMessage(E.ClassName+' '+ E.Message+' '+' open image in another image'+
    ' program then over write it - resave image so it loads properly'+
    ' Image Name: '+ ExtractFileName(ImageList.Strings[ImageCount]));
    ImageList.Delete(ImageCount);
   if (ImageCount >= ImageList.Count-1)
            then
              ImageCount := 0
            else
              ImageCount := ImageCount + 1;
  end;

  end;


end;


when it calls to cycle to an image I get that out of bounds error like the ImageList:TStringList;  never filled with files. but only in version 1.4.

when I run it in version 1.2 I do not get this error whatsoever. it loads all of the files in the directory than everything works as it should.

so now I am not knowing where to look to correct this problem. is it the TStringList that went batty?


any ideas on why this is throwing an error when it is trying to load an image from the ImageList in ver 1.4 and not 1.2?
« Last Edit: April 25, 2015, 10:51:00 pm by userx-bw »
My Finished Projects
https://sourceforge.net/projects/mhsetroot/
https://sourceforge.net/projects/gmhsetrootfreepascalfrontend/

HP Elitetbook 6930p Dual Core Intel vPro 2.66 gHz
VOID (Linux) 64bit

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #1 on: April 24, 2015, 09:37:17 pm »
What is the value of Form1.ImageCount? Just a curiosity why on earth you need to have 2 variables for the same thing?
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

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #2 on: April 24, 2015, 10:18:37 pm »
'*.jpg;_*.png' remove the underscored space first
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

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #3 on: April 24, 2015, 10:41:16 pm »
Code: [Select]
procedure FillImageList;
begin                                                                          //no sub directory -
  Form1.ImageList := FindAllFiles(ExtractFilePath(Form1.ImageFile), '*.jpg; *.png', false);
  Form1.ImageList.Sort;
end; 
This is not your problem, just to warn you:

"FindAllFiles" is a function which easily leads to memory leaks: It *creates* a stringlist, and when you assign it to Form1.ImageList the old list is lost producing a memory leak. Better call FreeAndNil(Form1.ImageList) before calling FindAllFiles. Or use the overloaded procedure of the same name in which the list is not created and which uses an existing list ("FindAllFiles(Form1.ImageList, ExtractFilePath(Form1.ImageFile), '*.jpg;'*.png', false)").

The other issue is that you use "Form1" within a method of TForm1. That's not good. It will lead to malfunction if you run another instance of TForm1 with a different name. The instance of TForm1 is automatically referenced in a method: in "TForm1.CycleImages" just call "ImageFile" instead of "Form1.ImageFile".
« Last Edit: April 24, 2015, 10:56:11 pm by wp »

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #4 on: April 24, 2015, 11:17:52 pm »
Can you attach your project (source only) or give a link to it so we can see your programs logic clearly?

Code: [Select]
procedure SetDir;
begin
       if not ( Form1.ImageList = nil ) then
     begin
      //Form1.ImageList.Free;

      FreeandNil(Form1.ImageList);

      Form1.ImageList := TStringList.Create;
      Form1.ImageCount:=0;
      end;   
......

Remove this section code and only leave Form1.ImageCount:=0;.

Code: [Select]
procedure FillImageList;
begin                                                                          //no sub directory -
  Form1.ImageList := FindAllFiles(ExtractFilePath(Form1.ImageFile), '*.jpg; *.png', false);
  Form1.ImageList.Sort;
end;

In this section of code, add FreeAndNil(Form1.ImageList); before Form1.ImageList := FindAllFiles... clause.

You should make FillImageList (also other subroutines) in to a class method if it accesses class variables/properties.
« Last Edit: April 24, 2015, 11:20:29 pm by Cyrax »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #5 on: April 24, 2015, 11:49:22 pm »
Here is a simplified unit that addresses the problems.
I renamed your ImageCount to ImageIndex, since it is an index, not a count, and removed your try...except clauses, since if your code gives rise to exceptions, they will be raised and shown anyway.

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Dialogs, ComCtrls, ExtCtrls, StdCtrls;

type

{ TForm1 }

  TForm1 = class(TForm)
    BCycle: TButton;
    BSetDir: TButton;
    Image1: TImage;
    StatusBar1: TStatusBar;
    procedure BCycleClick(Sender: TObject);
    procedure BSetDirClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    ImageList: TStringList;
    ImageIndex: integer;
    CurrentDir: string;
    procedure SetDir;
    procedure FillImageList;
    procedure CycleImages;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.BSetDirClick(Sender: TObject);
begin
  SetDir;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(ImageList);
end;

procedure TForm1.BCycleClick(Sender: TObject);
begin
  CycleImages;
end;

procedure TForm1.SetDir;
begin
  FreeAndNil(ImageList);
  ImageIndex:=0;
  if (SelectDirectory(CurrentDir, [sdPrompt], 0 ) = True) then
    begin
      SetCurrentDir(CurrentDir);
      FillImageList;
    end;
end;

procedure TForm1.FillImageList;
begin
  ImageList:=FindAllFiles(ExtractFilePath(CurrentDir), '*.jpg;*.png', False);
  ImageList.Sort;
end;

procedure TForm1.CycleImages;
begin
  if (ImageList = nil) then
  begin
    MessageDlg('No Directory is Set', mtWarning, [mbOK],0);
    SetDir;
  end;
  if (CompareFileExt(ImageList[ImageIndex], 'jpg') = 0 ) or
     (CompareFileExt(ImageList[ImageIndex], 'png') = 0 ) then
    begin
      if (ImageIndex >= ImageList.Count-1) then
        ImageIndex:=0
      else Inc(ImageIndex);
      if FileExists(ImageList[ImageIndex]) then
      begin
        Image1.Picture.LoadFromFile(ImageList[ImageIndex]);
        StatusBar1.SimpleText:=
          ExtractFileName(ImageList[ImageIndex]) +' ' +' Total ' +
            IntToStr(ImageList.Count);
        Caption:='Image# ' + IntToStr(ImageIndex)+'::'+ IntToStr(ImageList.Count)+' Total';
      end;
    end;
end;

end.



wp

  • Hero Member
  • *****
  • Posts: 11858
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #6 on: April 25, 2015, 12:16:47 am »
Here is a compilable project of Howard's code. It is working fine with me (after changing the ExtractFilePath(CurrentDir) in FindAllFiles to CurrentDir only).

@userx-bw:
If you want us to help you must post a full project which can be compiled and traced by the debugger. (only pas, lfm, lpi, lpr, packed into a single zip).

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #7 on: April 25, 2015, 12:36:34 am »
Where is this ImageFile declared?

Maybe you need do this to get desired result:
Code: [Select]
...
ImageFile := ImageList[ImageIndex];
...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #8 on: April 25, 2015, 06:12:38 am »
create a minimal test case that shows the problem and attach it in a message here, I'll take a closer look.
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

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #9 on: April 25, 2015, 09:28:36 am »
You state that this code
Code: [Select]
Form1.ImageList := FindAllFiles(ExtractFilePath(Form1.ImageFile), '*.jpg;*.png', false);
gives you an empty list.

The only reason I can see for this is that the value of Form1.ImageList is a directory with no jpg or png files in it.

Just do a ShowMessage(Form1.ImageList) before calling FindAllFiles and look if it is what you expected it to be.

And, as others have said: if you want us to help then attach the project here (all sources: lpi, lpr, lfm, pas) so we can see where it goes wrong.
Now we're just cristal balling.

Bart
« Last Edit: April 25, 2015, 03:02:36 pm by Bart »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #10 on: April 25, 2015, 03:02:15 pm »
Not realy related but I see lots of code like:

Code: [Select]
  if Stayontop.Checked = true

This is a rather redundant style of coding.
There is no need to compare the value of a boolean against true or false.
Just a simple if Stayontop.Checked is enough.
If it would not be enough then there would be no end to it, since (Stayontop.Checked = true) itself is a boolean, and by your programming logic it should be compared to either true or false, and so on.

Code: [Select]
  if ((((Stayontop.Checked = true)=true)=true)=true)=true) ... ad infinitum

Further more:

Code: [Select]
procedure TForm1.StayontopClick(Sender: TObject);
begin
  if Stayontop.Checked = false
    then
     begin
     Stayontop.Checked:=true;
     FormStyle:=fsStayOnTop;
     end
      else
  if Stayontop.Checked = true
       then
        begin
        Stayontop.Checked:=false;
        FormStyle:= fsNormal;
        end;
end;

Why the "if Stayontop.Checked = true" after the else, what other value could it possibly have, since you already know that it is not false.
AFAIK booleans don't come as true, false or maybe  ;)

This accomlishes the same and it reads a lot better:

Code: [Select]
procedure TForm1.StayontopClick(Sender: TObject);
begin
  if not Stayontop.Checked  then
     FormStyle:=fsStayOnTop;
  else
    FormStyle:= fsNormal;
  Stayontop.Checked:=not Stayontop.Checked;
end;

Bart

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #11 on: April 25, 2015, 03:43:37 pm »
userx-bw, come on, slow down! Please accept the advice of more experienced people, even if not related to your question - they have good reason to tell you.

The only reason why you did not yet get a solution for your problem is because you were not able to provide a compilable project which shows the error.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #12 on: April 25, 2015, 03:51:40 pm »
works too it DOES NOT MATTER either way will work and does work. your understanding and rigidness is making your  logic faulty and limiting your ways of thinking and living your entire life if you are applying this same way into your life as well.

2: what you tried to scold me for matters not. it is not an illegal function call so it gets no errors. only within that brain of yours does it throw an error. but not the compiler that has the final say. why is that?

Psychology now is in play here telling me you need to reboot your mind and stop trying to show off -- all your are doing is showing me how to type less to get the same results -- sweet ok -- but I did not post that question How do I make this shorter so I do not have to type as much and make it still work.
Bart was not showing off, and you do not need to feel offenced by his words. His thinking was not at all faulty. He was touching on the very principle of computer technology, namely transistors being switched either on or off, or in other words: BITS. Programming means that you write what is NECESSARY in order to turn switches on or off so that eventually the application does what you want it to do.

It is illogical and completely unnecessary to test

Code: [Select]
if red = red then ...
Bart is absolutely right on this even though it does not DIRECTLY solve your problem. Writing logic and clean code also helps others to help you. If you feel offenced by that, as you obviously do, you better not ask for help, IMHO.
« Last Edit: April 25, 2015, 03:58:05 pm by Artie »
keep it simple

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9794
  • Debugger - SynEdit - and more
    • wiki
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #13 on: April 25, 2015, 04:14:31 pm »
Lets try to track the actually problem.

The
Code: [Select]
List index (0) out of boundsis obviously due to a wrong initialization or usage of Form1.ImageCount

When setting it, you use it as count:
Code: [Select]
Form1.ImageCount:=0;but when using it, you use it like "high", highest element, that is count - 1:
Code: [Select]
ImageList.Strings[Form1.ImageCount]Because in an empty list (count = 0) there is no element with index 0.

There are more places that use it as "high":
Code: [Select]
ImageCount := ImageList.Count -1But even then I see no checks not to access ImageList.Strings, if the value is -1

So that, I would guess is broken in your code, and would fail with Lazarus 1.2 too, *IF* the list was empty.

----

If I understand, in Lazarus 1.2 the list is never empty.
While in Lazarus 1.4 it is empty, even though you supply the SAME PATH, and it has the same content?

Now then it was already pointed out that the likeliest candidate for this is
Code: [Select]
Form1.ImageList := FindAllFiles(ExtractFilePath(Form1.ImageFile), '*.jpg;*.png', false);
Now to find out what is causing it more data is needed:
- I may have missed it, but your OS is?
- The content of Form1.ImageFile is ?
- The result of "ExtractFilePath(Form1.ImageFile)" is?
- And after this "Form1.ImageList.Count" is 0 ?

To find the values, please add some lines to output (message dialog, logfile, console (debugln)...) the values above.

Maybe the path contains some sequence of chars, that triggers a problem?

One think I found by looking at the diff between 1.2 and 1.4 is that the possibility was added for FindAllFiles, to take a ";" separated list. (If I read the logs correct)
If I  understood it correct, that would break any code where the path actually contains a ";", because it would now be split as to paths.
There may be other changes, but I did not spot any (yet). So having actual data, that causes an issue is the next step.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: something is funny between 1.2 and 1.4 Lazarus
« Reply #14 on: April 25, 2015, 04:14:56 pm »
Well, if you don't want my assistence, then I'll back down.

Bart

 

TinyPortal © 2005-2018