Recent

Author Topic: [solved] TBitmap free or destroy  (Read 12980 times)

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TBitmap free or destroy
« Reply #15 on: February 01, 2013, 02:35:38 pm »
I made simple example, if i understood right:
Code: [Select]
type

  { TTestClass }

  TTestClass = class
  public
    sl: TStringList;
    constructor Create;
    destructor Destroy; override;
  end;

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    test: TTestClass;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  test:=TTestClass.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(test);
end;

{ TTestClass }

constructor TTestClass.Create;
begin
  sl:=TStringList.Create;
end;

destructor TTestClass.Destroy;
begin
  form1.test.sl.Free; // Crashes (uncommented)
  //sl.Free; // Works normally
  //self.sl.Free; // Also works normally
  inherited Destroy;
end;
« Last Edit: February 01, 2013, 04:17:19 pm by User137 »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TBitmap free or destroy
« Reply #16 on: February 01, 2013, 02:55:24 pm »
I made simple example, if i understood right:

An example of what? If this is directed to my request at Ask, then you have missed his point. here is what I understood.

Code: [Select]
type

  { TForm1 }

  TForm1 = class(TForm)
    Button1 : TButton;
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ShowMessage(Form1.Button1.Caption);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FreeAndNil(Form1);
end;


although the form and button are steel accessible and the showmessage will work as expected if you remove the "form1." when the event is raised the form1 variable is already nil so it will raise an exception.

Of course that example violates so many coding prιnciples that its not something you should encounter ever.
« Last Edit: February 01, 2013, 03:00:26 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TBitmap free or destroy
« Reply #17 on: February 01, 2013, 03:32:07 pm »
Isn't that exactly same thing... I just didn't go as far as to destroy form within its own methods, because that is way out of standard. So i just used a second class for it.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TBitmap free or destroy
« Reply #18 on: February 01, 2013, 03:46:31 pm »
no it would be the same thing if you accessed the Test private variable from the TestClass.destroy method.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: TBitmap free or destroy
« Reply #19 on: February 01, 2013, 03:55:04 pm »
I did
Code: [Select]
destructor TTestClass.Destroy;
begin
  //form1.test.sl.Free; // Crashes

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TBitmap free or destroy
« Reply #20 on: February 01, 2013, 04:07:23 pm »
sorry I'm used to ignore comment lines when speed reading, so I never show that line. In any case both examples violate coding principles its no something I expect to see and if I ever see such code in any library I would drop it immediately.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018