Autor Beitrag
FrankTheFox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Mi 04.06.08 20:13 
Hi,


ich habe mal ein Testprog. geschrieben um mich mit dem Visitordesignpattern
vertraut zu machen und um zu sehen was Delphi da macht.

Ich nutze eine simple Unit mit Formular und einem Button...

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Unit1;

type
  TForm4 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.Button1Click(Sender: TObject);
Var
  XmlDoc:TGmXmlObject;
 // XmlProlog: TGmXmlObject;
  XmlInterpreterVisitor: TXmlInterpreterVisitor;
begin
  XmlDoc:=TGmXmlObject.Create(ctXmlDoc);
 // XmlProlog:=TGmXmlObject.Create(ctXmlProlog);

  XmlInterpreterVisitor:=TXmlInterpreterVisitor.Create;

  XmlDoc.Accept(XmlInterpreterVisitor);
 // XmlProlog.Accept(XmlInterpreterVisitor);

  //XmlDoc.Free;
 // XmlProlog.Free;
  XmlInterpreterVisitor.Free;
end;

end.


dann eine Unit mit dem Visitor Zeugs


ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
unit Unit1;

interface

uses Dialogs, Classes, Controls;

Type

  TGmXmlObject = class;

  TGmClassType = (ctXmlDoc, ctXmlNode, ctXmlProlog,
                  ctXmlStartTag, ctXmlEndTag, ctXmlTagList);

  // Forward declaration of base visitor class
  IGmVisitor = interface;
  TXmlInterpreterVisitor = class;

  // Abstract base expression class
  TXmlExpression = class(TObject);

  // Equates to Client in the Interpreter pattern
  TXmlInterpreter = class(TObject);

  IGmXmlObject = interface
    procedure accept(Visitor: IGmVisitor);
    procedure SetClassType(Value: TGmClassType);
    function GetClassType(): TGmClassType;
  end;

  IGmVisitor = interface
    procedure visit(Exp: TGmXmlObject);
  end;

   TXmlInterpreterVisitor = class(TInterfacedObject, IgmVisitor)
   public
    procedure Visit(Exp : TGmXmlObject);
   end;

  TGmXmlObject = class(TInterfacedObject , IGmXmlObject)
  private
    FClassType: TGmClassType;
  public
    constructor create(ClassType: TGmClassType);
    procedure accept(Visitor: IGmVisitor);
    procedure SetClassType(Value: TGmClassType);
    function GetClassType(): TGmClassType;
  end;

implementation

procedure TXmlInterpreterVisitor.Visit(Exp: TGmXmlObject);
begin


  case exp.GetClassType of
    ctXmlDoc: ShowMessage('<TXMLDoc>');
    ctXmlProlog: ShowMessage('<TXMLProlog>');
    ctXmlNode: ShowMessage('<TXMLNode>');
  end;

end;

{ TGmXmlObject }

constructor TGmXmlObject.create(ClassType: TGmClassType);
begin
   inherited create;
   self.FClassType:= ClassType;
end;

function TGmXmlObject.GetClassType: TGmClassType;
begin
  Result:=FClassType;
end;

procedure TGmXmlObject.SetClassType(Value: TGmClassType);
begin
  FClassType:= value;
end;

procedure TGmXmlObject.Accept(Visitor : IGmVisitor);
begin
  Visitor.Visit(Self);
end;

end.


sobald der Visitor verlassen wird also de Teil mit ShowMessage('<TXMLDoc>')
bekomme ich den Fehler. Ich weiß allerdings nicht warum!
Vielleicht kann da mal jemand drüber schauen und mir sagen wo es klemmt. Wäre nett!

Gruß
Franky
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mi 04.06.08 20:33 
Wenn du eine Instanz einer Interface-Implementation einer Interface-Variablen zuordnest, darfst du das Objekt nicht mehr von Hand mit Free freigeben. Da deine Klasse von TInterfacedObject abgeleitet ist, wird das Objekt automatisch freigegeben, wenn die interface-Variable "out of scope" ist.

Ob das das Problem ist, weiss ich nicht, das ist aber sicher mal falsch.
FrankTheFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Mi 04.06.08 20:50 
Jupp,

ich habe alle Free's rausgeschmissen und das Problem taucht nicht mehr auf.
Daaaaannke. Ich denke das Problem ist gelöst.



Gruß
Franky
FrankTheFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Mi 04.06.08 20:57 
Titel: zu früh gefreut.......
wenn ich...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
procedure TForm4.Button1Click(Sender: TObject);
Var
  XmlDoc:TGmXmlObject;
  XmlProlog: TGmXmlObject;
  XmlInterpreterVisitor: TXmlInterpreterVisitor;
begin
  XmlDoc:=TGmXmlObject.Create(ctXmlDoc);
  XmlProlog:=TGmXmlObject.Create(ctXmlProlog);

  XmlInterpreterVisitor:=TXmlInterpreterVisitor.Create;

  XmlDoc.Accept(XmlInterpreterVisitor);
  XmlProlog.Accept(XmlInterpreterVisitor);
end;


habe ich das Problem immer noch!
FrankTheFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Mi 04.06.08 21:06 
So...Ich glaube ich habe das Problem eingekreist....



ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
procedure TForm4.Button1Click(Sender: TObject);
Var
  XmlDoc:TGmXmlObject;
  XmlProlog: TGmXmlObject;
  XmlEndTag: TGmXmlObject;
  XmlTagList: TGmXmlObject;
  XmlInterpreterVisitor: TXmlInterpreterVisitor;
begin
  XmlDoc := TGmXmlObject.Create(ctXmlDoc);
  XmlProlog := TGmXmlObject.Create(ctXmlProlog);
  XmlEndTag := TGmXmlObject.Create(ctXmlEndTag);
  XmlTagList := TGmXmlObject.Create(ctXmlTagList);

  
  XmlInterpreterVisitor:=TXmlInterpreterVisitor.Create;

  XmlDoc.Accept(XmlInterpreterVisitor);

  XmlInterpreterVisitor:=TXmlInterpreterVisitor.Create;

  XmlProlog.Accept(XmlInterpreterVisitor);

  XmlInterpreterVisitor:=TXmlInterpreterVisitor.Create;

  XmlEndTag.Accept(XmlInterpreterVisitor);

  XmlInterpreterVisitor:=TXmlInterpreterVisitor.Create;

  XmlTagList.Accept(XmlInterpreterVisitor);


end;


Das bedeutet ich muß den Visitor jedesmal neu erzeugen.......

So'n schiet...


Gruß

Franky
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Mi 04.06.08 23:18 
;) Du kannst dieselbe Objektreferenz nicht an zwei Interface-Variablen hintereinander zuweisen. Sobald du das Objekt einer Interface-Variablen zuweist und diese Variable out-of-scope ist, wird das Objekt zerstört. Bei dir findet eine implizite Zuweisung an eine Interface-Variable bei der Übergabe an Accept statt. Normalerweise weist man eine neue Instanz gleich nach dem Create einer Interface Variable zu. Dann hast du keine solchen Probleme. Wenn du keine Referenzzählung willst musst du deine Klasse nicht von TInterfacedObject ableiten.

Ersetz in deinem Fall in Button1Click XmlInterpreterVisitor: TXmlInterpreterVisitor; mal durch XmlInterpreterVisitor: IgmVisitor; dann sollte es eigentlich gehen.
FrankTheFox Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 51



BeitragVerfasst: Do 05.06.08 16:57 
Hi,


ok stimmt, jetzt gehts!! Danke!