Einzelnen Beitrag anzeigen

ford42

Registriert seit: 2. Okt 2015
60 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#1

Warum geht FreeAndNil nicht?

  Alt 6. Aug 2018, 10:34
Delphi-Version: 10.2 Tokyo
Hey,

wenn ich FreeAndNil in der Delete-Methode verwende bekomme ich einen Zugriffsfehler.

https://pastebin.com/9Rj84HKn

Weiß jemand wieso?


Vielen Dank
LG ford

[Edit: mkinzler]
Delphi-Quellcode:
unit UnitVerkettetListe;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  PNode = ^TNode;
  TNode = record
    data: String;
    previous: PNode;
    next: PNode;
  end;
  TForm1 = class(TForm)
    Lbl_Show: TLabel;
    Bt_Back: TButton;
    Bt_Insert: TButton;
    Bt_Vorward: TButton;
    Ed_Insert: TEdit;
    Bt_Delete: TButton;
    procedure Bt_InsertClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure RefreshButtons;
    procedure Bt_BackClick(Sender: TObject);
    procedure Bt_VorwardClick(Sender: TObject);
    procedure RefreshLbl;
    procedure Bt_DeleteClick(Sender: TObject);
    procedure Refresh;
  private
    { Private-Deklarationen }
    PCurrentNode: PNode;
    PFirstNode: PNode;
    PLastNode: PNode;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Bt_BackClick(Sender: TObject);
begin
  PCurrentNode := PCurrentNode.previous;
  Refresh;
end;

procedure TForm1.Bt_DeleteClick(Sender: TObject);
var
  PBufferNode: PNode;
begin
  if PCurrentNode.previous <> nil then
  begin
    PCurrentNode := PCurrentNode.previous;
    PBufferNode := PCurrentNode.next;
    if PCurrentNode.next.next <> nil then
    begin
      PCurrentNode.next := PCurrentNode.next.next;
    end
    else
    begin
      PCurrentNode.next := nil;
    end;
    if PBufferNode.next <> nil then
    begin
      PBufferNode.next.previous := PBufferNode.previous;
    end;
    PBufferNode := nil;
    Refresh;
    //PBufferNode.previous := nil;
    //PBufferNode.next := nil;
    //FreeAndNil(PBufferNode);
  end
  else if PCurrentNode.next <> nil then
  begin
    PCurrentNode := PCurrentNode.next;
    PBufferNode := PCurrentNode.previous;
    if PCurrentNode.previous.previous <> nil then
    begin
      PCurrentNode.previous := PCurrentNode.previous.previous;
    end
    else
    begin
      PCurrentNode.previous := nil;
    end;
  PBufferNode := nil;
  Refresh;
  end
  else
  begin
    PCurrentNode := nil;
    Bt_Back.Enabled := False;
    Bt_Vorward.Enabled := False;
    RefreshLbl;
  end;
end;

procedure TForm1.Bt_InsertClick(Sender: TObject);
begin
  New(PCurrentNode);
  PCurrentNode.data := Ed_Insert.Text;
  Ed_Insert.Text := '';
  PCurrentNode.previous := nil;
  PCurrentNode.next := nil;

  if PFirstNode = nil then
  begin
    PFirstNode := PCurrentNode;
  end
  else
  begin
    PLastNode.next := PCurrentNode;
    PCurrentNode.previous := PLastNode;
  end;
  PLastNode := PCurrentNode;
  Refresh;

end;

procedure TForm1.Bt_VorwardClick(Sender: TObject);
begin
 PCurrentNode := PCurrentNode.next;
 Refresh;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 Bt_Back.Enabled := False;
 Bt_Vorward.Enabled := False;
 PFirstNode := nil;
 PLastNode := nil;
end;

procedure TForm1.RefreshButtons;
begin
  if PCurrentNode.previous <> nil then
  begin
    Bt_Back.Enabled := True;
  end
  else
  begin
    Bt_Back.Enabled := False;
  end;

  if PCurrentNode.next <> nil then
  begin
    Bt_Vorward.Enabled := True;
  end
  else
  begin
    Bt_Vorward.Enabled := False;
  end;

end;

procedure TForm1.RefreshLbl;
begin
  if PCurrentNode <> nil then
  begin
    Lbl_Show.Caption := PCurrentNode.data;
  end
  else
  begin
    Lbl_Show.Caption := '';
  end;
end;

procedure TForm1.Refresh;
begin
  RefreshButtons;
  RefreshLbl;
end;


end.
[/edit]

Geändert von mkinzler ( 6. Aug 2018 um 10:42 Uhr) Grund: Besser hier anhängen.
  Mit Zitat antworten Zitat