Recent

Author Topic: Access Violation on FormCreate  (Read 4409 times)

lennit

  • Jr. Member
  • **
  • Posts: 63
  • Aquila non capit muscas.
Access Violation on FormCreate
« on: August 01, 2015, 07:09:34 am »
Hello,

I get AV error on FormCreate while testing the program at another computer, while at the one the program is complied there is no error and it loads fine. All that the FormCreate event does is load data from some files and fill in some arrays. All files are present at the both places, and anyway I get a different error message if a file is missing. It shows the error message without any address and after I cancel the loading of the program it shows the message attached. The line 18 in question is FormCreate. If I press OK (and risk data corruption  8-) ) then it never starts and the process is soon killed. The target system is Win 7 32bit while I compile at Win 7 64bit with Lazarus 32bit.
Win 7 32/64
Win 10 64bit
Lazarus 1.4.4
Firebird 2.5.3 (x64)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation on FormCreate
« Reply #1 on: August 01, 2015, 08:42:09 am »
To get help debugging your program you need to share compilable code that produces the error (not just an error message).
Maybe a cut-down version with a couple of data files with dummy data that also shows the problem?

lennit

  • Jr. Member
  • **
  • Posts: 63
  • Aquila non capit muscas.
Re: Access Violation on FormCreate
« Reply #2 on: August 01, 2015, 09:34:57 am »
This is the piece of code that stalls all. Attached are sample files. They are archived because of the number of files limit/ the file type limit.

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  off11.Click;

  SysUtils.FormatSettings.DecimalSeparator := '.';
  DTS1.DateTimeFormat:='hh:mm'+#13#10+'DD.MM.';

  for i:=1 to 11 do begin
    for j:=1 to 2 do begin
      pingair[i,j]:=true;
      pingheat[i,j]:=true;
      pingvent[i,j]:=true;
      pingcool[i,j]:=true;
    end;
    for j:=1 to 10 do
      pingsensor[i,j]:=true;
  end;

  for i:=1 to 11 do
   goTimer[i]:=false;

  edit52.passwordchar := '*';
  edit53.passwordchar := '*';
  edit54.passwordchar := '*';


  for i := 1 to 10 do
    a[i] := True;
  for i := 1 to 31 do
    pingres[i] := True;
   for i := 1 to 11 do
    nosu6ene[i] := false;

   host[1]:='192.168.1.101';
   host[2]:='192.168.1.102';
   host[3]:='192.168.1.103';
   host[4]:='192.168.1.104';
   host[5]:='192.168.1.105';
   host[6]:='192.168.1.106';
   host[7]:='192.168.1.107';
   host[8]:='192.168.1.108';
   host[9]:='192.168.1.109';
   host[10]:='192.168.1.110';
   host[11]:='192.168.1.111';
   host[12]:='192.168.1.112';
   host[13]:='192.168.1.113';
   host[14]:='192.168.1.114';
   host[15]:='192.168.1.115';
   host[16]:='192.168.1.116';
   host[17]:='192.168.1.117';
   host[18]:='192.168.1.118';
   host[19]:='192.168.1.153';
   host[20]:='192.168.1.177';
   host[21]:='';
   host[22]:='';
   host[23]:='192.168.1.215';
   host[24]:='192.168.1.217';
   host[25]:='192.168.1.218';
   host[26]:='192.168.1.220';
   host[27]:='192.168.1.225';
   host[28]:='192.168.1.226';
   host[29]:='192.168.1.227';
   host[30]:='192.168.1.230';
   host[31]:='192.168.1.220';

   servlist.Clear;
  pathString:=ExtractFilePath(Application.ExeName);
  pathString:=pathString+'\data\setobr';
  if FindFirst(pathString,faAnyFile, FindIt)=0 then begin
    servlist.items.loadfromfile(pathString);
    for i := 1 to 10 do
    begin
    sRecord := servlist.Items[i - 1];
    ParseRecordOBR(sRecord, i);
    end;
  end;
  FindClose(FindIt);
  servlist.Clear;
  pathString:=ExtractFilePath(Application.ExeName);
  pathString:=pathString+'\data\znil';
  if FindFirst(pathString,faAnyFile, FindIt)=0 then begin
    servlist.items.loadfromfile(pathString);
    for i := 1 to 10 do
    begin
    sRecord := servlist.Items[i - 1];
    ParseRecordNIL(sRecord, i);
    end;
  end;
  FindClose(FindIt);
  //password
  servlist.Clear;
  pathString:=ExtractFilePath(Application.ExeName);
  pathString:=pathString+'\data\rpm1';
  if FindFirst(pathString,faAnyFile, FindIt)=0 then begin
  servlist.items.loadfromfile(pathString);
  label256.caption := servlist.Items[0];
  end;
  FindClose(FindIt);

  j := 0;
  for i := 0 to 23 do
    for n := 0 to 5 do
    begin
      j := j + 1;
      HH := i;
      MM := 10 * n;
      SS := 00;
      MS := 00;
      chas[j] := encodetime(HH, MM, SS, MS);
    end;
 //regime
 for n := 1 to 11 do
    for m := 1 to 8 do
      for i := 1 to 20 do
        regime[n, m, i] := 0;
  for n := 1 to 11 do
  begin
    listbox5.Clear;
    FN := 'regimesetR' + IntToStr(n);
    pathString:=ExtractFilePath(Application.ExeName);
    pathString:=pathString+'\data\'+FN;
    if FindFirst(pathString,faAnyFile, FindIt)=0 then begin
      listbox5.items.loadfromfile(pathstring);
      m := n;
      for i := 1 to 4 do
      begin
        sRecord := ListBox5.Items[i - 1];
        ParseRecordREG(sRecord, i);
      end;           // 4.
    end;
   end;
  listbox5.Clear;
  pathString:=ExtractFilePath(Application.ExeName);
  pathString:=pathString+'\data\general';
  if FindFirst(pathString,faAnyFile, FindIt)=0 then
    begin
  sRecord := StringReplace(sRecord, '"', '', [rfReplaceAll]);
  Col := 1;
  repeat
    PosComma := Pos(',', sRecord);
    if PosComma > 0 then
      sField := Copy(sRecord, 1, PosComma - 1)
    else
      sField := sRecord;
    generalset[Col]:= strtoint(sField);
    if PosComma > 0 then
    begin
      Delete(sRecord, 1, PosComma);
      Col := Col + 1;
    end;
  until (PosComma = 0);
  case generalset[1] of
    1:radiobutton1.Checked:=true;
    2:radiobutton2.Checked:=true;
    3:radiobutton3.Checked:=true;
  end;
  priority1.ItemIndex:=generalset[2]-1;
  priority2.ItemIndex:=generalset[3]-1;
  priority3.ItemIndex:=generalset[4]-1;
  priority4.ItemIndex:=generalset[5]-1;
  priority5.ItemIndex:=generalset[6]-1;
  priority6.ItemIndex:=generalset[7]-1;
  priority7.ItemIndex:=generalset[8]-1;
  priority8.ItemIndex:=generalset[9]-1;
  priority9.ItemIndex:=generalset[10]-1;
end;

 listbox4.Clear;
  pathString:=ExtractFilePath(Application.ExeName);
  pathString:=pathString+'\data\netsettings.csv;
  if FindFirst(pathString,faAnyFile, FindIt)=0 then
    begin
    listbox4.items.loadfromfile(pathstring);
    for i := 1 to 12 do
    begin
      sRecord := ListBox4.Items[i - 1];
      ParseRecordSET(sRecord, i);             // 4.
    end;
  end
  else
    ShowMessage('Не са намерени настройки за мрежата!');       

  //restart
  for i:=1 to 12 do
    for j:=1 to 144 do
       for n:=1 to 20 do
           danni[i,j,n]:=0;
  for i:=1 to 2 do
    for j:=1 to 144 do
       for n:=1 to 20 do
           dataINC[i,j,n]:=0;
  P := 'C:\Mushroom_Master_beta';
  for i := 1 to 12 do
  begin
    FN := '';
    listbox3.Clear;
    decodedate(Date, yy, mon, dd);
    if (mon > 9) and (dd > 9) then
      FN := IntToStr(yy) + IntToStr(mon) + IntToStr(dd);
    if (mon > 9) and (dd <= 9) then
      FN := IntToStr(yy) + IntToStr(mon) + '0' + IntToStr(dd);
    if (mon <= 9) and (dd > 9) then
      FN := IntToStr(yy) + '0' + IntToStr(mon) + IntToStr(dd);
    if (mon <= 9) and (dd <= 9) then
      FN := IntToStr(yy) + '0' + IntToStr(mon) + '0' + IntToStr(dd);
    FN := FN + 'R' + IntToStr(i);
    pathString:=ExtractFilePath(Application.ExeName);
    pathString:=pathString+'\chronology\'+FN;
    if FindFirst(pathString,faAnyFile, FindIt)=0 then begin
      ListBox3.Items.LoadFromFile(pathstring);
      for j := 1 to 144 do
      begin
        n := j - 1;
        sRecord := ListBox3.Items[n];
        m := 1;
        repeat
          PosComma := Pos(',', sRecord);
          if PosComma > 0 then
            sField := Copy(sRecord, 1, PosComma - 1)
          else
            sField := sRecord;
          case m of
            2: danni[i, j, 1] := strtofloat(sField);
            3: danni[i, j, 2] := strtofloat(sField);
            4: danni[i, j, 3] := strtofloat(sField);
            5: danni[i, j, 4] := strtofloat(sField);
            6: danni[i, j, 5] := strtofloat(sField);
            7: danni[i, j, 6] := strtofloat(sField);
            8: danni[i, j, 7] := strtofloat(sField);
            9: danni[i, j, 8] := strtofloat(sField);
            10: danni[i, j, 9] := strtofloat(sField);
            11: danni[i, j, 10] := strtofloat(sField);
            12: danni[i, j, 11] := strtofloat(sField);
            13: danni[i, j, 12] := strtofloat(sField);
            14: danni[i, j, 13] := strtofloat(sField);
            15: danni[i, j, 14] := strtofloat(sField);
            16: danni[i, j, 15] := strtofloat(sField);
            17: danni[i, j, 16] := strtofloat(sField);
            18: danni[i, j, 17] := strtofloat(sField);
            19: danni[i, j, 18] := strtofloat(sField);
            20: danni[i, j, 19] := strtofloat(sField);
            21: danni[i, j, 20] := strtofloat(sField);
          end;
          if PosComma > 0 then
          begin
            Delete(sRecord, 1, PosComma);
            m := m + 1;
          end;
        until PosComma = 0;
      room[i,j].c1:=danni[i, j, 1];
      room[i,j].c2:=danni[i, j, 2];
      room[i,j].cav:=danni[i, j, 3];
      room[i,j].air:=danni[i, j, 4];
      room[i,j].rh:=danni[i, j, 5];
      room[i,j].co2:=danni[i, j, 6];
      end;
  end;
   end;
  for i:=10 to 1 do
    for j:=1 to 144 do
       for n:=1 to 20 do
           dataINC[i,j,n]:=danni[i,j,n];
  startlog.Enabled:=true;
end;   
Win 7 32/64
Win 10 64bit
Lazarus 1.4.4
Firebird 2.5.3 (x64)

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
Re: Access Violation on FormCreate
« Reply #3 on: August 01, 2015, 10:20:01 am »
Try turn on range checking, rebuild and run.

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Access Violation on FormCreate
« Reply #4 on: August 01, 2015, 10:45:04 am »
Are your arrays static or dynamic arrays? Remember that dynamic array index starts with 0.

lennit

  • Jr. Member
  • **
  • Posts: 63
  • Aquila non capit muscas.
Re: Access Violation on FormCreate
« Reply #5 on: August 01, 2015, 10:53:13 am »
I use both, but in this procedure all are static. As of now range checking turns fine.
Win 7 32/64
Win 10 64bit
Lazarus 1.4.4
Firebird 2.5.3 (x64)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Access Violation on FormCreate
« Reply #6 on: August 01, 2015, 12:15:10 pm »
You are very trusting of your data. Code sections such as
Code: [Select]
if FindFirst(pathString,faAnyFile, FindIt)=0 then begin
    servlist.items.loadfromfile(pathString);
    for i := 1 to 10 do
    begin
    sRecord := servlist.Items[i - 1];
    ParseRecordNIL(sRecord, i);
    end;
  end;                           
should at least check that servlist.items.Count is higher than 10.

For the AV: what happens when you single step through your long OnCreate code in the debugger? Which line of code produces the AV?
You haven't provided compilable code, so we can't try this.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Access Violation on FormCreate
« Reply #7 on: August 01, 2015, 12:26:55 pm »
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  off11.Click;
You begin the FormCreate by calling a event-procedure of one of it's components? It's best you put everything you have in that off11Click procedure and put it in a separate procedure with a more descriptive name. You also need to make sure it doesn't do anything that is not allowed before the creation (and showing) of the form.

You are using a really strange construct to check if the file exists:
Code: [Select]
  if FindFirst(pathString,faAnyFile, FindIt)=0 then
  begin
    // do something
  end;
  FindClose(FindIt);
I don't see you using the FindIt variabel anywhere so there is no need to do FindFirst. Also... you forgot the FindClose with the last 2 FindFirst you used. So why not just use FileExists from SysUtils?
Code: [Select]
  if SysUtils.FileExists(pathString) then
  begin
    // do something
  end;

For the AV: what happens when you single step through your long OnCreate code in the debugger? Which line of code produces the AV?
You haven't provided compilable code, so we can't try this.
lennit has the problem on another PC. Not the PC where it is compiled. So the debugger can't be used.

@lennit: If the machine that's giving you problems you might want to try putting in some ShowMessage (poor mans debugging :)). That way you can narrow down the problem line.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Access Violation on FormCreate
« Reply #8 on: August 03, 2015, 11:04:56 am »
That OnCreate stuff should be in an override of loaded.

Like:
Code: [Select]
procedure TYourForm.Loaded;Override;
begin
// your "OnCreate" stuff here and then:
inherited loaded;
end;

Does that give more info?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

lennit

  • Jr. Member
  • **
  • Posts: 63
  • Aquila non capit muscas.
Re: Access Violation on FormCreate
« Reply #9 on: October 26, 2015, 11:50:57 am »
Thank you for all the replies! That bug disappeared as it appeared - suddenly and without a trace. Poetry aside, that same client PC keeps giving me on random basis similar bugs, crashing the software on pressing a button for example and showing the errors in the attachments below.
Is it possible that a bug arises because this app was initially written on the good old Lazarus 1.1 and with certain changes (and many times going through Publish Project command) it's now compiled under 1.4.4? I realize this is a very general question, but I am tired of dealing with things that show up and fade away without any action on my side.  :-[ I need to learn proper error handling... after I finish this project  :D at least this is what I say every time and never have time to actually do it.
Win 7 32/64
Win 10 64bit
Lazarus 1.4.4
Firebird 2.5.3 (x64)

 

TinyPortal © 2005-2018