Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stack overflow when press button delphi

I'm build an application that reducing the pixels width.
When I'm pressing the button of that application two or three times, Message will appear and say stack overflow.

Here's the Message :
Popup Message


Error Line on 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 my application keep says overflow? is there any solution to fix it? or can we can exception handler? thanks

EDIT
Now my pplication says access violation.
Access Violation


It raised error in this line : nb[7] := imgval[x-1,y+1]; why it exactly happened?

like image 462
Then Theresia Avatar asked May 20 '26 14:05

Then Theresia


1 Answers

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 have size 501*501*4 = 1,004,004. The default stack size is 1MB. These large arrays are the reason for your stack overflow.

You will need to use dynamically allocated arrays instead. Or avoid the need to store 2D arrays that contain information for each pixel and instead process the image in smaller sub-blocks. I've no idea whether or not that is possible because I've no idea what the code is trying to do. That's for you to work out.

Of course, one advantage of using dynamically allocated arrays is that you don't need to run the gauntlet of a buffer overrun, as you currently do. If either dimension of the image exceeds 501 then you have overrun the buffer. I do hope that you have enabled range checking in the compiler options.

for y := 0 to baris_gbr-1 do

and

for x := 0 to kolom_gbr-1 do

cannot be correct. The baris_gbr and kolom_gbr variables are not initialised since they were most recently used as loop variables. So, as well as turning on range checking, you'll want to turn on hints and warnings, and then heed them.

like image 106
David Heffernan Avatar answered May 23 '26 04:05

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!