Autor Beitrag
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Sa 01.03.08 21:35 
HI,

ich bin grad dabei mir n textadventure zu schrieben, hab heute angefangen.
auf jedenfal hab ich mir viele verschiedene typen erstellt, die alle ineinander
verschachtelt sind. wenn ich die jetzt per create aufruf kommt n speicherfehler.
siehe Code:

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:
unit Unit1;

interface

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

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

  TTest2 = class
  private
    fName: String;
  public
    constructor Create(Name: String);
  end;

  TArray = array of TTest2;
  TTest = class
  private
    fName: String;
    fArray: TArray;
    fArrayCount: Integer;
  public
    constructor Create(Name: String);
    property PArray: TArray read fArray write fArray;
    procedure add(test:TTest2);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var t1: TTest;

constructor TTest2.Create(Name: String);
begin
  inherited Create;   //erzeuge
  fname := name;      //übergebe name
end;

constructor TTest.Create(Name: String);
begin
  inherited Create;  //erzeuge
  fname := Name;     //übergebe name
  fArrayCount := 0;  //setze counter = 0
end;

procedure TTest.add(test: TTest2);
begin
  inc(fArrayCount);   //erhöhe counter
  SetLength(fArray,fArrayCount); //setze array länge (reserviere speicher)
  fArray[fArrayCount-1].Create(test.fName); //erzeuge sub-typ im array   ?!HIER IS DER FEHLER?!
end;

procedure TForm1.Button1Click(Sender: TObject);
var t2: TTest2;
begin
  t1 := TTest.Create('1');  //erzeuge haupt-typ
  t2 := TTest2.Create('2'); //erzeuge sub-typ
  t1.add(t2);       //trage den sub-typ im haupt-typ ein
  t2.Destroy;      //gebe speicher für sub-typ frei
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Button2.Caption := t1.PArray[0].fName;  //zeige mir den namen, des 1. sub-typs im haupt-typ
end;

end.


kann mir da jmd helfen?

MfG & Thx Bergmann

p.s. die typen sind zur zeit bloß zu testzwecken so aufgestellt!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 01.03.08 21:46 
Du darfst den Speicher einer Instanz mit Destroy\Free erst dann wieder freigeben, wenn Du diese nicht mehr benötigst.

Ein Aufruf von Destroy sollte ferner vermieden werden; benutze lieber Free oder FreeAndNil.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Bergmann89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Sa 01.03.08 21:56 
ok, das werd ich noch ändern, aber der fehler muss
irgendwo in dem array sein, wenn ich an stelle [0]
dann typ erzeugen will kommt der speicherfehler!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 01.03.08 22:00 
Ich übersetze mal kurz:
ausblenden Delphi-Quelltext
1:
  fArray[fArrayCount-1].Create(test.fName); //erzeuge sub-typ im array   ?!HIER IS DER FEHLER?!					

bedeutet aus der Sicht von Delphi so viel wie:
ausblenden Delphi-Quelltext
1:
  TTest2(nil).Create(test.fName); //erzeuge sub-typ im array   ?!HIER IS DER FEHLER?!					

was man auch grob übersetzen könnte mit:
ausblenden Quelltext
1:
Greif ins Nix und mach was draus ...					


Vielleicht solltest Du mal das so hier probieren:
ausblenden Delphi-Quelltext
1:
  fArray[fArrayCount-1] := TTest2.Create(test.fName); //erzeuge sub-typ im array   ?!HIER IS DER FEHLER?!					


Gelle ;-)

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Bergmann89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Sa 01.03.08 22:07 
achja^^ stimmt! schön blöd xD
und nochwas anders zu dem nilandfree;
kann ich das irgendwie mit in den typ einbauen,
das ich nich jedesmal "FreeAndNil(VAR)" schreiben muss,
sondern einfach VAR.FreeAndNil??

€:
habs gefunden... einfach:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TTest2.Free;
begin
  FreeAndNil(self);
end;


oder spricht das was dagegen?
Man ich steh heute ganz schön auf'm schlauh, vlt.
sollte ich huete ma zeitig ins bett^^
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 01.03.08 22:34 
Jup, dagegen spricht der Sinn von FreeAndNil ;-)

Die Funktion FreeAndNil gibt nämlich die übergebene Objekt-Referenz frei und setzt dann die Variable, in der diese enthalten war, auf nil. Wenn Du obigen Source von Dir verwendest, so wird der zweite wichtige Schritt, nämlich das Entfernen der ungültigen Referenz nicht ausgeführt, wodurch man auch nur Obj.Free hätte aufrufen können.

Grad in deinem Fall ist aber dieser zusätzliche Effekt, dass die Referenzen korrekt auf Nil gesetzt werden, wichtig, da Du somit einfach bestimmte Fälle abprüfen kannst, ob ein bestimmtes Objekt noch existiert oder bereits freigegeben wurde.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Bergmann89 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Sa 01.03.08 22:55 
moment, nich so schnell...
bsp:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
var t: Ttest;

t := Ttest.create;
f.free //nach meinem code oben


wenn ich das so machen würde, dann gibt er zwar den speicher frei,
aber denk das in der variable t immer noch was enthalten ist,
richtig so?!
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 01.03.08 23:33 
Korrekt. Und mit der Funktion FreeAndNil hast Du nach dem Freigeben einen definierten Wert (nämlich nil in t stehen und kannst auf diesen Prüfen, um festzustellen, ob t noch auf eine Instanz zeigt.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.