Stack overflow on delphi button click

I am creating an application that shrinks the pixel width.
When I click this app button two or three times, a message pops up and says Stack Overflow.

Here's the message:
Popup Message


Error line in my application
Error on application
Here's my code:

procedure TForm1.cariThin();
var
  baris_gbr, kolom_gbr, x, y, a, b, i, j, p1, p2, n : integer;
  imgval : array [0..500,0..500] of integer;
  mark : array [0..500,0..500] of integer;
  nb : array [1..9] of integer;
  hasdelete: boolean;
  R, G, BL, AB : integer;
begin
  Image3.Width := Image1.Width;
  Image3.Height := Image1.Height;

  baris_gbr := Image1.Picture.Height;
  kolom_gbr := Image1.Picture.Width;


  For kolom_gbr:= 0 To image1.Width - 1 Do
  Begin
    For baris_gbr:= 0 To image1.Height - 1 Do
     Begin
      R:= GetRValue(image1.Canvas.Pixels[kolom_gbr, baris_gbr]);
      G:= GetGValue(image1.Canvas.Pixels[kolom_gbr, baris_gbr]);
      BL:= GetBValue(image1.Canvas.Pixels[kolom_gbr, baris_gbr]);
      AB:= (R + G + BL) Div 3;

      if (AB > 200) then
      begin
        Image1.Canvas.Pixels[kolom_gbr, baris_gbr] := rgb(255,255,255);
      end
      else
      begin
        Image1.Canvas.Pixels[kolom_gbr, baris_gbr] := rgb(0,0,0);
      end;
    End;
  End;

  for y := 0 to baris_gbr-1 do
  begin
    for x := 0 to kolom_gbr-1 do
    begin
      if (Image1.canvas.pixels[x,y] = clBlack) then
      begin
        imgval[x,y] := 1;
      end
      else
      begin
         imgval[x,y] := 0;
      end;
    end;
  end;

  hasdelete := True;
  while (hasdelete) do
  begin
    hasdelete := False;
    for y := 0 to baris_gbr-1 do
    begin
     for x := 0 to kolom_gbr-1 do
     begin
        if (imgval[x,y] = 1) then
        begin
          for n:=1 to 8 do
          begin
            nb[n] := 0;
            nb[1] := imgval[x,y];
            nb[2] := imgval[x,y-1];
            nb[3] := imgval[x+1,y-1];
            nb[4] := imgval[x+1,y];
            nb[5] := imgval[x+1,y+1];
            nb[6] := imgval[x,y+1];
            nb[7] := imgval[x-1,y+1];
            nb[8] := imgval[x-1,y];
            nb[9] := imgval[x-1,y-1];
            a := 0;
          end;

          for i:= 2 to 8 do
          begin
            if ((nb[i] = 0) AND (nb[i+1] = 1)) then
            begin
              inc(a);
            end;
          end;

          if ((nb[9] = 0) AND (nb[2] = 1)) then
          begin
            inc(a);
          end;

          b := nb[2] + nb[3] + nb[4] + nb[5] + nb[6] + nb[7] + nb[8] + nb[9];
          p1 := nb[2] * nb[4] * nb[6];
          p2 := nb[4] * nb[6] * nb[8];

          if ((a = 1) AND ((b>=2) AND (b <= 6)) AND (p1 = 0) AND (p2 = 0)) then
          begin
            mark[x,y] := 0;
            hasdelete := true;
          end
          else
          begin
            mark[x,y] := 1;
          end
        end
        else
        begin
          mark[x,y] := 0;
        end;
      end;
    end;

    for y:=0 to baris_gbr-1 do
    begin
      for x:=0 to kolom_gbr-1 do
      begin
        imgval[x,y] := mark[x,y];
      end;
    end;
  end;
end;

      

Why does my application keep talking about overflow? is there any solution to fix it? or can we have an exception handler? thank

EDIT
My expression is now talking about an access violation.
Access Violation


He raised an error on this line: nb [7]: = imgval [x-1, y + 1]; why did this happen?

+3


source to share


1 answer


var
  imgval : array [0..500,0..500] of integer;
  mark : array [0..500,0..500] of integer;

      

These variables are located on the stack and are huge. They are 501 * 501 * 4 = 1 004 004. The default stack size is 1 MB. These large arrays are the reason.

You will need to use dynamically allocated arrays instead. Or you don't need to store 2D arrays containing information for each pixel and instead process the image in smaller sub-blocks. I have no idea if this is possible because I have no idea what the code is trying to do. This is for you to work out.

Of course, one of the benefits of using dynamically allocated arrays is that you don't need to run the glove of buffer overflows as you do right now. If the image size exceeds 501, you overflow the buffer. I hope you have included range checking in the compiler options.



for y := 0 to baris_gbr-1 do

      

and

for x := 0 to kolom_gbr-1 do

      

can't be right. Variables baris_gbr

and are kolom_gbr

not initialized as they were recently used as loop variables. So, as well as enabling range checking, you'll want to enable prompts and warnings and then listen for them.

+5


source







All Articles