Autor Beitrag
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 13.06.06 10:53 
hi, mal ne frage ^^ wenn ich destroy als destructor deklariere, dann wird dieser doch auch beim aufruf von free aufgerufen oder nicht,
warum muss ich destroy mit override deklarieren ?

ist das korrekt so ?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
type
  TSimpleObject = class
    constructor Create;
    destructor Destroy; override;
  private
    Fposition,
    Fsize: TVector2D;
    Fz: integer;
    Fangle: single;
    Fvisible: boolean;
    Fcolor: TColor;
    procedure SetAngle(const value: single);
  public
    procedure Draw;
    property position: TVector2D read Fposition write Fposition;
    property size: TVector2D read Fsize write Fsize;
    property z: integer read Fz write Fz;
    property angle: single read Fangle write SetAngle;
    property visble: boolean read Fvisible write Fvisible;
    property color: TColor read Fcolor write FColor;
  end;


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



BeitragVerfasst: Di 13.06.06 11:09 
Damit TObject.Free *deinen* Destroyer erwischt und nicht den der Oberklasse.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
  TObject = class
// ...
    destructor Destroy; virtual;

// ...

procedure TObject.Free;
begin
  if Self <> nil then
    Destroy; // <-- Hier wird *zuerst* dein Destruktor aufgerufen und nicht der von TObject.Destroy. 
             // Deine Klasse ruft dann mittels inherited die Destruktoren der Oberklassen auf. 
end;
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 13.06.06 11:27 
was passiert wenn ich nur destroy aufrufe ? reicht das, oder muss ich free nehmen ? dann wäre das ja sozusagen pflicht ^^ ich möchte in meiner engine ja keine speicherlöcher erzeugen. (ok passiert selbst valve mal, aber net in so einer minimalistischen engine)
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 13.06.06 11:29 
Wieso weshalb und warum steht alles in der Hilfe unter Free. Der Unterschied ist lediglich, dass TObject(nil).Free; keine Fehlermeldung ergibt, TObject(nil).Destroy hingegen schon.
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 13.06.06 11:32 
ich hab das nur noch net janz verstanden, wenn ich jetzt angenommen noch free einbaue muss ich das ja auch erstmal deklarieren, soll ich das denn overriden, dann inherited.free am ende aufrufen oder wie läuft das?
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 13.06.06 11:36 
Nein. Free überschreibst du nicht. Das .Free ist immer verfügbar, da sie in der TObject deklariert ist. Wenn du MeineKlasse.Free aufrufst dann wird automatisch TObject.Free ausgeführt.
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 13.06.06 11:40 
und self ist nur nil, wenn das objekt nicht erstellt wurde ?
weil ich ja noch unterobjekte erstelle die in destroy freigegeben werden

und jetzt (durch das virtual) habe ich:
ausblenden Delphi-Quelltext
1:
[Warnung] UTextureManager.pas(12): W1010 Methode 'Destroy' verbirgt virtuelle Methode vom Basistyp 'TObject'					
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 13.06.06 11:52 
Du hast wohl vergessen Destroy mit override zu markieren (Fehlermeldung)
F34r0fTh3D4rk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Di 13.06.06 12:04 
dann kann ich aber kein virtual nehmen
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: Di 13.06.06 12:46 
Du musst die oberste Methode virtual und alle anderen overriden.