Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-01

Is an object instanciated?

2 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Johann Glaser

Hi!

In my program I have a class:

Type CTest = class
               { ... }
               Constructor Create;
             End;

This class will be held in a variable:

Var Test : CTest;

The class is generated:

  Test := CTest.Create;

But I don't know, _when_ it is generated, because this will be done when the user selects a menu entry. When the user now again selects the menu entry, I have to check, wether the variable "Test" already contains a created class or not.

At the moment I have solved that problem by setting Test to Nil
  Test := Nil;
at the beginning of my program and then testing
  if Text = Nil then
    Test := CTest.Create;

Is there another, more beautiful way to check if a class is already created?

What can I do if the class "Destroy()"s itself (e.g. if that class would be an "About" window with an "OK" button which closes the window) but doesn't tell the main program that it is destroyed?

Thanks
Hansi

Full_Name
to Johann Glaser

Quoting Johann Glaser :

The solution (you're half way there) is as follows..

 Type
   TTest = class(TObject)
     { ... }
     constructor Create;
   end;
  var
    Test : TTest = nil;
  {...}
  procedure TestTest;
  begin
    if assigned(Test) then begin { or 'if Test  nil then ...' }
      writeln('Test is instanciated')
      Test.Free;
    end
    else begin
      writeln('Test is nil');
      Test := TTest.Create;
    end;
    if assigned(Test) then { or 'if Test  nil then ...' }
      writeln('Test is instanciated')
    else
      writeln('Test is nil');
  end;

When you 'free' (*not* 'Destroy') your class, set it to 'Nil' (this is *not* done for you.)

  begin
     //free class
     Test.Free;
     Test := Nil;
  end;

If you do this, next time you test 'assigned' you will get the truth. Assigned simply tests whether the variable is equal to Nil (more or less).

Another tip, classes do *not* destroy themselves. Another piece of code must 'free' them..

e.g.

  procedure TTest1.FreeUpResources;
  begin
    //Test2 free class
    Test2.Free;
    Test2 := Nil;
  end;

You therefore *know* when something has been free'd because you called the 'free' method yourself. FPC does *not* have garbage collection, so every class you Create, you must call the 'Free' method for, or a memory leak will be caused.

One final tip. Do *not* use 'global' variables. Make all of your variable part of your main class.. (it is better to use solely Object Oriented code rather than a bad mix of procedural and OO...)

e.g.

  type
    TSubclass = class(TObject)
      {...}
    end;
    TMainClass = class(TObject)
    protected
      Subclass: TSubclass;
    public
      constructor Create; virtual;
      destructor  Destroy; override;
    end;
  {...}
  constructor TMainClass.Create;
  begin
    Subclass := TSubclass.Create;
  end;
  destructor TMainClass.Destroy;
  begin
    Subclass.Free;
    //always call this last...
    inherited Destroy;
  end;

Hope that helps,

Matt

Show quoted text 36 lines
Hi! In my program I have a class: Type CTest = class { ... } Constructor Create; End; This class will be held in a variable: Var Test : CTest; The class is generated: Test := CTest.Create; But I don't know, _when_ it is generated, because this will be done when the user selects a menu entry. When the user now again selects the menu entry, I have to check, wether the variable "Test" already contains a created class or not. At the moment I have solved that problem by setting Test to Nil Test := Nil; at the beginning of my program and then testing if Text = Nil then Test := CTest.Create; Is there another, more beautiful way to check if a class is already created? What can I do if the class "Destroy()"s itself (e.g. if that class would be an "About" window with an "OK" button which closes the window) but doesn't tell the main program that it is destroyed?
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." Kristian Wilson, Nintendo, Inc, 1989 -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+ G e++ h--- r+++ y+++ ------END GEEK CODE BLOCK------