Recent

Author Topic: [SOLVED] Error creating a new class  (Read 4787 times)

pusuni

  • Jr. Member
  • **
  • Posts: 67
[SOLVED] Error creating a new class
« on: October 22, 2014, 01:02:05 pm »
Hi I've got a problem with  new class creation

I wrote the following code  in a different unit called Objetos2:
Code: [Select]
unit Objetos2;

{$mode objfpc}{$H+}

interface

uses Classes;


type

  TTest = class(TObject)
  private
     { Private declarations }
     FXYZ               :  real;
     procedure SetXYZX (const dato : real );
  protected
     { Protected declarations }
  public
     { Public declarations }
     property XYZX : real read FXYZ write SetXYZX;
  published
     { Published declarations }
  end;

implementation


procedure TTest.SetXYZX (const dato : real );
begin
   FXYZ := dato;
end;

end.

With Lazarus Pascal, I created a new form,  and in  the "create event"  I add the following code:

  test.create;
  test.XYZX := 1;

where test is of the TTest type;

The complete code for Unit1 is:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Objetos2;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  test : TTest;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  test.create;
  test.XYZX := 1;
end;

end.

The code compiled Ok, but If I run the program, it hangs. I get the following code  'External:SIGSEGV' exception in line:

   FXYZ := dato;

from Object2 unit.

Any suggestion?
Thank in advanced
« Last Edit: October 23, 2014, 10:28:12 am by pusuni »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10484
  • FPC developer.
Re: Error creating a new class
« Reply #1 on: October 22, 2014, 01:08:08 pm »
A class creation looks like:

Code: [Select]
  test:=TTest.create;

Your code only calls the constructor part of the class construction sequence, and doesn't reserve memory.

pusuni

  • Jr. Member
  • **
  • Posts: 67
Re: Error creating a new class
« Reply #2 on: October 22, 2014, 03:15:59 pm »
Hi marcov. Thanks for your answer. I imagined that subject, but there is one thing I don't understand now.

 If i change the code,  and the new class TTest  is child  from TEdit, for example,
Code: [Select]
unit Objetos2;

{$mode objfpc}{$H+}

interface

uses Classes, StdCtrls;


type

  TTest = class(TEdit)
  private
     { Private declarations }
     FXYZ               :  real;
     procedure SetXYZX (const dato : real );
  protected
     { Protected declarations }
  public
     { Public declarations }
     property XYZX : real read FXYZ write SetXYZX;
  published
     { Published declarations }
  end;

implementation


procedure TTest.SetXYZX (const dato : real );
begin
   FXYZ := dato;
end;

end.
                                   

The program still crashes,  why do I need to reserve memory, yet?


I have a lot of controls  made for me, for LCL/IDE - I don't use new/dispose commands - and they work gracefully.

This is part of code of one control (It is a derivative control of TCustomEdit) and the constructor doesn't reserve memory for the new variables:
Code: [Select]
type
   tNumero = ( real, entero );
   tBaseNumero = ( Bin, Oct, Dec, Hex );
   TOnEffects = procedure (const estado : BOOLEAN) of Object;
   TNumberEdit = class(TCustomEdit)
   private
      { Private declarations }
      Fmode                                    : tNumero;
      FNumberBase                              : tBaseNumero;
      FNumberNegative                          : BOOLEAN;
      FColorError                              : TColor;
      FColorErrorTime                          : cardinal;
      ColorTMP                                 : TColor;
      Timer1                                   : TTimer;
      FSoundError                              : BOOLEAN;
      FFocusFont                               : TFont;
      FTempFont                                : TFont;
      FValueInteger                            : Int64;
      FValueReal                               : extended;
      FOnError                                 : TNotifyEvent;
      FOnChangeNumber                          : TNotifyEvent;
      FOnOverflowNumber                        : TNotifyEvent;
      FOnEffects                               : TOnEffects;
      NumeroPrevioReal                         : extended;
      NumeroPrevioEntero                       : Int64;
      UnDoNumero                               : string;
      UnDoNumero2                              : string;
      PosicionCursorUnDo                       : integer;
      PosicionCursorUnDo2                      : integer;
      ActivadoUnDo                             : BOOLEAN;
      ActivadoEfecto                           : BOOLEAN;
      procedure CambiaColor (Sender : TObject);
      procedure RestauraColor (Sender : TObject);
      procedure CadenaAReal;
      procedure CadenaAEntero;
      procedure SetFocusFont(AValue: TFont);
      procedure GuardaValorParaUnDo;
   protected


........






constructor TNumberEdit.Create(AOwner : TComponent);
begin
   inherited Create (AOwner);
   FMode                := real;
   FNumberBase          := Dec;
   FNumberNegative      := TRUE;
   FColorError          := clDefault;
   Timer1               := TTimer.Create (Self);
   Timer1.OnStartTimer  := @CambiaColor;
   Timer1.OnTimer       := @RestauraColor;
   Timer1.Enabled       := FALSE;
   FFocusFont           := TFont.Create;
   FFocusFont.Assign(Self.Font);
   FFocusFont.Name      := 'Comic Sans MS';
   FFocusFont.Size      := 14;
   FTempFont            := TFont.Create;
   ActivadoEfecto       := FALSE;
   ActivadoUnDo         := FALSE;
end;


Where was made the memory resever for the new variables?: FMode,    ActivadoEfecto , FNumberBase ...

¿LCL  made the reserve of memory?


Thanks again

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Error creating a new class
« Reply #3 on: October 22, 2014, 03:32:36 pm »
...
I have a lot of controls  made for me, for LCL/IDE - I don't use new/dispose commands - and they work gracefully.
...

For these controls, LCL calls automatically their constructors and put their memory addresses to respective variables. If you need to manually create your control somewhere in your code and access its properties/methods,  you need to declare a variable which is based on your class type. Just like in marcovs example.

rvk

  • Hero Member
  • *****
  • Posts: 5070
Re: Error creating a new class
« Reply #4 on: October 22, 2014, 03:33:55 pm »
¿LCL  made the reserve of memory?
No... (almost) none of your used variables (tBaseNumero, etc) are pointer variable. So there is no need for memory reservation. The variable itself is the memory.

However, I see a TTimer in there.
Are you telling me you can't find a Timer1 := TTimer.Create in your TNumberEdit source anywhere??
(Maybe it's not in TNumberEdit.Create but it should be somewhere because TTimer is also a pointer-variable and you can't use it without a TTimer.Create.)

Edit: I also see some TFont variable. These should be created in your TNumberEdit somewhere.

Edit2: Aah. I see it already ("Timer1 := TTimer.Create (Self)" and "FFocusFont := TFont.Create")
So you see that simple variables (like Integer, Real, records, TColor etc) don't need creating but TTimer and TFont do need .Create to reserve memory for them)
« Last Edit: October 22, 2014, 03:40:58 pm by rvk »

pusuni

  • Jr. Member
  • **
  • Posts: 67
Re: Error creating a new class
« Reply #5 on: October 23, 2014, 10:27:02 am »
A class creation looks like:

Code: [Select]
  test:=TTest.create;

Your code only calls the constructor part of the class construction sequence, and doesn't reserve memory.

marcov, I do apologize for idiot question. Yesterday I was not a great day for me

Thanks at all

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 10484
  • FPC developer.
Re: Error creating a new class
« Reply #6 on: October 23, 2014, 01:13:36 pm »
marcov, I do apologize for idiot question. Yesterday I was not a great day for me

No apology necessary, we all have such days :_)

 

TinyPortal © 2005-2018