Sign in Join

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

Mailing lists / fpc-pascal / 2014-01

gtree - Freeing a nil Root without error

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Daniel Gaspary

Hi,

gtree.TTree type has the following code:

constructor TTree.Create;
begin
  FRoot := nil;
end;
destructor TTree.Destroy;
begin
  FRoot.Free;
end;

How is possible then that the following program don't raise an error when the destructor (via free) is called?

program Project1;
{$mode objfpc}{$H+}
uses
  Classes, gtree;
type
    TStrTree = specialize TTree<String>;
var
   tt: TStrTree;
begin
     tt:=TStrTree.Create;
     if tt.Root = nil
     then
         WriteLn('Root is nil!');
     tt.Free;
end.

Thanks,

Daniel

Marco van de Voort
to Daniel Gaspary

In our previous episode, Daniel Gaspary said:

gtree.TTree type has the following code: How is possible then that the following program don't raise an error when the destructor (via free) is called?

Free only calls the destructor if self isn't nil.

      procedure TObject.Free;
        begin
           // the call via self avoids a warning
           if selfnil then
             self.destroy;
        end;
Daniel Gaspary
to Marco van de Voort
Marco van de Voort wrote
Free only calls the destructor if self isn't nil.

Thank you, Marco.

I was searching in stl package for a "Graph" type. is there such type?

Daniel Gaspary
to Daniel Gaspary
Daniel Gaspary wrote
Thank you, Marco. I was searching in stl package for a "Graph" type. is there such type?

Answering to myself...

I believe the Graph types exist only in Lazarus LazControls package, unit LvlGraphCtrl.

leledumbo
to Daniel Gaspary

Actually, I'm planning on redesigning gtree to be a part of larger ggraph unit. As in the data structure theory, a tree is just a graph without cycle. So I guess it should be made that way. There are a lot of graph and tree variations though, so I think I need to take some time to design before implementing. Besides, I'm a bit busy these days.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/gtree-Freeing-a-nil-Root-without-error-tp5717832p5717836.html Sent from the Free Pascal - General mailing list .
Daniel Gaspary
to leledumbo
leledumbo wrote
As in the data structure theory, a tree is just a graph without cycle. So I guess it should be made that way. There are a lot of graph and tree variations though, so I think I need to take some time to design before implementing.

I don't know if the edges information can not be irrelevant and even an obstacle to better performance in trees.

Anyway, good to know these plans.

Thank you.