Access violation in Delphi after successful run -


i have written program in delphi compute, display , save pascals' triangle user-defined number of rows. works fine (displays triangle, , allows me save it), except comes access violation @ end! here message:

access violation @ address 004031db in module 'project1.exe'. read of address 00000000.

i have 2d dynamic array in procedure release memory @ end (:= nil). why still giving me access violation? frustrating!

i searched archives answer not find appropriate answer. appreciated.

here code (i little hesitant there bit of code:

 procedure tform1.btnptclick(sender: tobject);  var    i, j, k, n, midcol: integer;    pt: array of array of integer;    row: string;  begin    k := strtoint(lblnumrows.text);    n := strtoint(lblnumrows.text);//# values in row = row number     try        //initiatlize array        setlength(pt, k, (n*2)-1);        := 0 k-1          j := 0 (n*2-1)            pt[i,j] := 0;         midcol := (n*2 div 2)-1;//midcol 0-based        := 0 k-1          begin            if (i = 0)              pt[i,midcol] := 1//first row gets 1 in middle column                else if = 1                  begin                    pt[i,midcol-1] := 1;                    pt[i,midcol+1] := 1; //first , last value in second = 1                  end             else //if other row               begin                  //middle column                 pt[i, midcol] := pt[i-1,midcol-1] + pt[i-1,midcol+1];                 //right triangle                 j := midcol+1 (n*2-1)                   begin                     if (pt[i-1, j-1]=1) then//if @ end of prev row                       begin                         pt[i,j] := 1;                         break;                       end                     else                       pt[i,j] := pt[i-1,j-1] + pt[i-1,j+1];                   end;                 //left triangle                 j := midcol-1 downto 0                   begin                     if (pt[i-1, j+1] = 1) //if @ end of prev row                       begin                         pt[i,j] := 1;                         break;                       end                     else                       pt[i,j] := pt[i-1,j-1] + pt[i-1,j+1];                   end;               end;          end;           //now add pyramid memo          application.processmessages;          := 0 k-1            begin              row := '';              j := 0 n*2-1                begin                  if (pt[i,j] = 0) row := row + ' '                  else row := row + inttostr(pt[i,j]);                end;              memo.lines.add(row);            end;          setlength(pt, 0, 0);    end;  end; 

read of address 00000000 

this indicates trying access memory using pointer nil. know why 1 need code. @ present have code, , can explain.

run program in debugger. enable debug dcus in case error raised in rtl/vcl code. ensure debugger configured break on exceptions. run program , trigger error. debugger show nil object being de-referenced. have work out why reference nil.

the code have added answer has buffer overrun explain problem. setlength incorrect , should read:

setlength(pt, k, n*2); 

your code writes memory out-of-bounds , corrupts heap. should ask compiler produce runtime checks on array bounds. enable compiler's range checking option. had done so, have found error yourself.

for worth not need try/finally block since compiler automatically insert hidden block. there's no need 2 when 1 suffices. dynamic array managed type memory disposed when variable leaves scope.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -