Problems with the destructor lazarus / 61-Beginners ---------------------------------------------------------------------- krull · 2017-05-18 17:33 hello! I still program my vertical shoot 'em up. Now I'm at the point to handle collides. if I make FreeAndNil(self) I catch External Sigsegv. If I use destroy() it brakes in the destructor. I really don't know which fault i made. Many thanks if you know. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Windows ,Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, BGRABitmap, BGRABitmapTypes; type  TPlayer = class    playerPosTimer : TTimer;    shotTimer: TTimer;    constructor create();    procedure playerPosTimerTimer(Sender: TObject);    procedure playerPaint(p:Point;pOld:Point);    procedure shotTimerTimer(Sender: TObject);    procedure shotPaint(shot:TBGRABitmap; p:Point);    procedure takeDamage(a:Integer);    destructor destroy();  private   public   playerShip : TBGRABitmap;   shot1, shot2, shot3, shot4: TBGRABitmap;   pPlayer : Point;   pFormer: Point;   shotSpeed : Integer;   pShot1, pShot2, pShot3, pShot4 : Point;   leftClick, x : boolean;   Health: Integer;  end;   TEnemy1 = class    enemy1PosTimer : TTimer;    enemy1ShotTimer : TTimer;    constructor create(s:integer);    procedure enemy1PosTimerTimer(Sender: TObject);    procedure enemy1shotTimerTimer(Sender: TObject);    procedure enemy1Paint(p:Point;pOld:Point);    procedure enemy1ShotPaint();    procedure takeDamage(a:Integer);    destructor destroy();  private   public    enemy1Ship : TBGRABitmap;    pEnemy1, pEnemy1Former: Point;    shotSpeed, enemySpeed : Integer;    strategy: String;    Health: Integer;  end;   { TForm1 }   TForm1 = class(TForm)    procedure FormClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);   private    { private declarations }  public    { public declarations }  end; var  Form1: TForm1;  Player1 : TPlayer;  Enemy1 : TEnemy1;  Enemy2 : TEnemy1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);begin Player1:=T Player.create; Enemy1:=TEnemy1.create(1); Enemy2:=TEnemy1.create(2);en d; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin  if (Key=VK_ESCAPE) then halt(0)end;  procedure TForm1.FormClick(Sender: TObject);begin  Player1.leftClick:=true;end; constructor TPlayer.Create();begin  playerShip := TBGRABitmap.Create('ship0.png');  pPlayer:= Mouse.CursorPos;  pPlayer:= Form1.ScreenToClient(pPlayer);  shotSpeed:=10;  health:=2;  leftClick:= false;  pShot1.y:=1; pShot2.y:=1; pShot3.y:=1; pShot4.y:=1;  playerPosTimer := TTimer.Create(nil);  shotTimer := TTimer.create(nil);  playerPosTimer.Interval := 25;  shotTimer.Interval := 25;  playerPosTimer.OnTimer := @playerPosTimerTimer;  shotTimer.onTimer := @shotTimerTimer;  playerPosTimer.Enabled := true;  shotTimer.Enabled := true;end; procedure TPlayer.playerPosTimerTimer(Sender: TObject);begin  pFormer:= pPlayer;  pPlayer:= Mouse.CursorPos;  pPlayer:= Form1.ScreenToClient(pPlayer);  if pPlayer.x < pFormer.x then  begin    playerShip.free;    playerShip := TBGRABitmap.Create('ship-4.png');  end    else if pPlayer.x > pFormer.x then    begin    playerShip.free;    playerShip := TBGRABitmap.Create('ship4.png');    end    else    begin      playerShip.free;      playerShip := TBGRABitmap.Create('ship0.png');  end;  playerPaint(PPlayer,pFormer);end;   procedure TPlayer.playerPaint(p:Point;pOld:Point);begin  Form1.Canvas.FillRect(p Old.x-round(0.5*playerShip.Width),pOld.y- round(0.5*playerShip.Height),pOld.x+playerShip.Width- round(0.5*playerShip.Width),pOld.y+playerShip.Height- round(0.5*playerShip.Height));  playerShip.Draw(Form1.Canvas, p.x- round(0.5*playerShip.Width),p.y- round(0.5*playerShip.Height),false);end; procedure TPlayer.shotTimerTimer(Sender: TObject);begin  if (leftClick = true) and not assigned(shot1) then  begin    shot1:= TBGRABitmap.Create('Beam1.png');    pShot1.x:=pPlayer.x- round(0.5*shot1.width);    pShot1.y:=pPlayer.y- round(0.5*playerShip.Height)-shot1.Height;    shotPaint(shot1,pShot1);    leftClick:=false;  end;  if (leftClick = true) and not assigned(shot2) and assigned(shot1) then  begin    shot2:= TBGRABitmap.Create('Beam1.png');    pShot2.x:=pPlayer.x- round(0.5*shot2.width);    pShot2.y:=pPlayer.y- round(0.5*playerShip.Height)-shot2.Height;    shotPaint(shot2,pShot2);    leftClick:=false;  end;  if (leftClick = true) and not assigned(shot3) and assigned(shot2) and assigned(shot1)then  begin    shot3:= TBGRABitmap.Create('Beam1.png');    pShot3.x:=pPlayer.x- round(0.5*shot3.width);    pShot3.y:=pPlayer.y- round(0.5*playerShip.Height)-shot3.Height;    shotPaint(shot3,pShot3);    leftClick:=false;  end;  if (leftClick = true) and assigned(shot1) and assigned(shot2) and assigned(shot3) and not assigned(shot4) then  begin    shot4:= TBGRABitmap.Create('Beam1.png');    pShot4.x:=pPlayer.x- round(0.5*shot4.width);    pShot4.y:=pPlayer.y- round(0.5*playerShip.Height)-shot4.Height;    shotPaint(shot4,pShot4);    leftClick:=false;  end;   if assigned(shot1) or assigned(shot2) or assigned(shot3) or assigned(shot4) then  begin    if pShot1.y<=0 then    begin      Form1.canvas.FillRect(pShot1.x, pShot1.y, pShot1.x+shot1.Width, pShot1.y+shot1.Height);      FreeAndNil(shot1);      pShot1.y:=1;    end    else if assigned(shot1) then    begin      pShot1.y:= pShot1.y-shotSpeed;      shotPaint(shot1,pShot1);    end;    if (pShot2.y <= 0) then    begin      Form1.canvas.FillRect(pShot2.x, pShot2.y, pShot2.x+shot2.Width, pShot2.y+shot2.Height);      FreeAndNil(shot2);      pShot2.y:=1;    end    else if assigned(shot2) then    begin      pShot2.y:= pShot2.y-shotSpeed;      shotPaint(shot2,pShot2);    end;    if (pShot3.y <= 0) then    begin    Form1.canvas.FillRect(pShot3.x, pShot3.y, pShot3.x+shot3.Width, pShot3.y+shot3.Height);      FreeANdNil(shot3);      pShot3.y:=1;    end    else if assigned(shot3) then    begin      pShot3.y:= pShot3.y-shotSpeed;      shotPaint(shot3,pShot3);    end;    if (pShot4.y <= 0) then    begin      Form1.canvas.FillRect(pShot4.x, pShot4.y, pShot4.x+shot4.Width, pShot4.y+shot4.Height);      FreeANdNil(shot4);      pShot4.y:=1;    end    else if assigned(shot4) then    begin      pShot4.y:= pShot4.y-shotSpeed;      shotPaint(shot4,pShot4);    end;  end; end; procedure TPlayer.shotPaint(shot:TBGRABitmap; p:Point);begin Form1.canvas.FillRect(p.x, p.y+shotSpeed, p.x+shot.Width, p.y+shot.Height+shotSpeed); shot.draw(Form1.Canvas,p.x ,p.y,true);end; procedure TPlayer.takeDamage(a:Integer);begin end; destructor TPLayer.destroy();begin end; constructor TEnemy1.create(s:integer);begin if s=1 then strategy:='Attack' else strategy:='Defense';  enemy1Ship:=TBGRABitmap.Create('Enemy1.png');  shotSpeed:=15;  enemySpeed:=4;  health:=1;  enemy1PosTimer:=TTimer.create(nil);  enemy1ShotTimer:=TTimer.create(nil);  enemy1PosTimer.Interval:= 25;  enemy1ShotTimer.Interval:= 25;  enemy1PosTimer.onTimer:= @enemy1PosTimerTimer;  enemy1ShotTimer.onTimer:= @enemy1shotTimerTimer;  enemy1PosTimer.Enabled:=true;  enemy1ShotTimer.Enabled:=true;  PEnemy1.y:=0; pEnemy1.x:=round(0.5*Form1.Width);  enemy1Ship.Draw(Form1.Canvas,pEnemy1.x,pEnemy1.y,false);end;procedure TEnemy1.enemy1PosTimerTimer(Sender: TObject);label 1;var a,g,h : Double;begin  if strategy='Attack' then  begin    if (Player1.pPlayer.x-pEnemy1.x>0) or (Player1.pPlayer.x-pEnemy1.x<0)then    begin      a:=arctan((Player1.p Player.y-pEnemy1.y+200)/(Player1.pPlayer.x-pEnemy1.x));      g:=sin(a)*enemySpeed;      if g=0 then Goto 1;      h:=sqrt(sqr(enemySpeed))-(sqr(g));      if (pEnemy1.x-Player1.pPlayer.x < 0) and (pEnemy1.y-Player1.pPlayer.y+200 < 0) then      begin        pEnemy1Former:=pEnemy1; pEnemy1.x:=pEnemy1.x+abs(round(h)); pEnemy1.y:=pEnemy1.y+abs(round(g));        enemy1Paint(pEnemy1,pEnemy1Former);        Goto 1;      end      else if (pEnemy1.x-Player1.pPlayer.x < 0) and (pEnemy1.y-Player1.pPlayer.y+200 > 0) then      begin        pEnemy1Former:=pEnemy1; pEnemy1.x:=pEnemy1.x+abs(round(h)); pEnemy1.y:=pEnemy1.y-abs(round(g));        enemy1Paint(pEnemy1,pEnemy1Former);        Goto 1;      end      else if (pEnemy1.x-Player1.pPlayer.x > 0) and (pEnemy1.y-Player1.pPlayer.y+200< 0) then      begin        pEnemy1Former:=pEnemy1; pEnemy1.x:=pEnemy1.x-abs(round(h)); pEnemy1.y:=pEnemy1.y+abs(round(g));        enemy1Paint(pEnemy1,pEnemy1Former);        Goto 1;      end      else if (pEnemy1.x-Player1.pPlayer.x > 0) and (pEnemy1.y-Player1.pPlayer.y+200 >0) then      begin        pEnemy1Former:=pEnemy1; pEnemy1.x:=pEnemy1.x-abs(round(h)); pEnemy1.y:=pEnemy1.y-abs(round(g));        enemy1Paint(pEnemy1,pEnemy1Former);        Goto 1;      end;    end;  end;   if strategy='Defense' then  begin    if pEnemy1.y<100 then    begin      pEnemy1Former.x:=pEnemy1.x;      pEnemy1Former.y:=pEnemy1.y;      pEnemy1.y:=pEnemy1.y+EnemySpeed;    enemy1Paint(pEnemy1,pEnemy1Former);      Goto 1;    end;    if pEnemy1.x<=0 then    begin      pEnemy1Former.x:=pEnemy1.x;      pEnemy1Former.y:=pEnemy1.y;      pEnemy1.x:=pEnemy1.x+EnemySpeed;    enemy1Paint(pEnemy1,pEnemy1Former);      Goto 1;    end;    if pEnemy1.x>=Form1.width then    begin      pEnemy1Former.x:=pEnemy1.x;      pEnemy1Former.y:=pEnemy1.y;      pEnemy1.x:=pEnemy1.x-EnemySpeed;      enemy1Paint(pEnemy1,pEnemy1Former);      Goto 1;    end;    if pEnemy1.x=round(0.5*Form1.Width) then    begin      Randomize;      if random(1)>0 then      begin        pEnemy1Former.x:=pEnemy1.x;        pEnemy1Former.y:=pEnemy1.y;        pEnemy1.x:=pEnemy1.x+EnemySpeed;      enemy1Paint(pEnemy1,pEnemy1Former);        Goto 1;      end      else      begin        pEnemy1Former.x:=pEnemy1.x;        pEnemy1Former.y:=pEnemy1.y;        pEnemy1.x:=pEnemy1.x-EnemySpeed;      enemy1Paint(pEnemy1,pEnemy1Former);        Goto 1;      end;    end;    if pEnemy1.x>pEnemy1Former.x then    begin      pEnemy1Former.x:=pEnemy1.x;      pEnemy1Former.y:=pEnemy1.y;      pEnemy1.x:=pEnemy1.x+EnemySpeed;      enemy1Paint(pEnemy1,pEnemy1Former);      Goto 1;    end    else if pEnemy1.x