Recent

Author Topic: SOLVED!!! - What is wrong with this code  (Read 3997 times)

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
SOLVED!!! - What is wrong with this code
« on: February 19, 2017, 04:45:56 pm »
Hi, i made a simple program, this work fine on Windows and Linux, but on macOS (Carbon y cocoa) raise a exception on click

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.     procedure Button2Click(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.   Button2: TButton;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.Button1Click(Sender: TObject);
  35. begin
  36.    Button2:=TButton.Create(Form1);
  37.    Button2.Parent:=Form1;
  38.    Button2.Caption:='Release';
  39.    Button2.OnClick:=@Button2Click;
  40.    Button2.Left:=0;
  41.    Button2.top:=0;
  42. end;
  43.  
  44. procedure TForm1.Button2Click(Sender: TObject);
  45. begin
  46.    Button2.Free;
  47. end;
  48.  
  49. end.
  50.  
  51.  
« Last Edit: February 19, 2017, 06:46:54 pm by esvignolo »

balazsszekely

  • Guest
Re: What is wrong with this code
« Reply #1 on: February 19, 2017, 04:54:21 pm »
Freeing an object from inside its own event is not a "healthy" thing to do. It's like a software hara-kiri.

jacmoe

  • Full Member
  • ***
  • Posts: 249
    • Jacmoe's Cyber SoapBox
Re: What is wrong with this code
« Reply #2 on: February 19, 2017, 04:56:47 pm »
More like software-banzai.  ;)

Why not simply:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Close;
  4. end;
  5.  
« Last Edit: February 19, 2017, 05:06:37 pm by jacmoe »
more signal - less noise

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: What is wrong with this code
« Reply #3 on: February 19, 2017, 05:37:24 pm »
The odd thing is that your code didn't raise an exception on Windows and Linux.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
Re: What is wrong with this code
« Reply #4 on: February 19, 2017, 05:43:34 pm »
Because this is sample, the real thing is more complex, the problem is, i need a button when i pressed, the button must be destroyed.

More like software-banzai.  ;)

Why not simply:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   Close;
  4. end;
  5.  

jacmoe

  • Full Member
  • ***
  • Posts: 249
    • Jacmoe's Cyber SoapBox
Re: What is wrong with this code
« Reply #5 on: February 19, 2017, 05:47:58 pm »
When you close the form, everything associated with that form is destroyed because the owner has left the building.
Unless you have created the objects dynamically (AFAIK).
more signal - less noise

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: What is wrong with this code
« Reply #6 on: February 19, 2017, 05:49:09 pm »
Because this is sample, the real thing is more complex, the problem is, i need a button when i pressed, the button must be destroyed.

Try
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  Application.ReleaseComponent(Sender);
end;

It will ensure that the releasing of the buttons is not done in the buttons's onclick event.
You should not destroy an object if it is stll performing something (like an event).

Bart

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
Re: What is wrong with this code
« Reply #7 on: February 19, 2017, 05:51:54 pm »
Thanks Bart! i supuse the same thing, but i don't know how do it. :) I will try.

 

TinyPortal © 2005-2018