Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - Stringlist als Rueckgabe einer Funktion


Aiko07 - Sa 17.05.03 14:36
Titel: Stringlist als Rueckgabe einer Funktion
Hallo,

habe als Anfänger Problem mit einer Funktion. Diese Funktion möchte ich für Stringbearbeitungen nutzen. Bei der Rückgabe läuft folgender Fehler auf "Access violation at adress....."

Danke im voraus,
Bernd

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:
procedure test1;
function test(source :string): TstringList;

implementation

{$R *.dfm}
procedure test1;
 var
 sliste: TStringList;
 source: string;
begin
source:='C:\WINDOWS\Desktop\krcUploader\delTest\Config.dat';
sliste:=Tstringlist.Create;
sliste:=test(source);
sliste.SaveToFile('C:\WINDOWS\Desktop\krcUploader\delTest\Config.txt');
sliste.Free;
end;

function test(source :string): TstringList;
 var List1: TStringList;
begin
  List1:=TStringList.Create;
   try
    List1.LoadFromFile(source);
      .
      .
      .
    result.Assign(List1);

  finally
     List1.Free;

  end;
end;


Keldorn - Sa 17.05.03 17:46

Hallo

so geht das nicht.
Das Problem ist, das Result der Funktion eine TStringlist ist, die nie erstellt wird. Wenn du sie erstellst (result:=Tstringlist.create) schaffst du dir ein Speicherloch, da du diese Stringlist schwer wieder freigeben kannst.
Um dieses Problem zu umgehen, nimm statt der Funktion eine Procedure und arbeite mit der Stringlist als Var Paramter


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure test(source :string;VAR SL: TstringList);
var List1: TStringList;
begin
  List1:=TStringList.Create;
   try
    List1.LoadFromFile(source);
      .
      .
      .
    SL.Assign(List1);

  finally
     List1.Free;

  end;
end


//Aufruf:
sliste:=Tstringlist.Create;
test(source,SLliste);

obwohl du das in deinem Beispiel so nicht brauchst. (geht ja auch gleich sl.Loadfromfile...)

Mfg Frank


Aiko07 - So 18.05.03 16:18

Vieln Dank Frank,

mein Gedanke war diese Funktion in verschiedenen Proceduren zu nutzen, um aus Strg-Listen Blöcke zu kopieren und diese als Rückgabewert in andere Listen übergeben.

M.f.g.
Bernd


Delete - So 18.05.03 16:59

Aus einer News Group:
Zitat:
On 17 May 2003, "Andreas Kienle" <andreaskienle@gmx.de> wrote:

> Hi, I've got a question and hope you can help me...
>
> I have a function which looks like that:
>
> function any(idea: TIdea) : TStrings;
> begin
> Result := TStringList.Create;
> Result.Add('no idea');
> ...
> end;
>
> This works (on the surface). But don't I have to call a "Result.Free"
> somewhere to free the memory? But where? If I do it within the
> function then I'll lose the result and else I can't access the
> internal variable :/
In this situation it would be up to the caller of the function to free the
string list. Many people frown on having a function return an object for
this reason; it's not always clear who's responsible for freeing the
object.

Lösungen:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
function GetStrings : TStringList;
begin
  Result := TStringList.Create;
  Result.Add('no idea');
end;

procedure TForm1.Button1Click(Sender: TObject);
var sl : tStringList;
begin
  sl := GetStrings;
  caption := IntToStr(sl.Count);
  sl.Free;
end;



Delphi-Quelltext
1:
2:
3:
Blah := any(Idea);
// do something with Blah
Blah.Free;


Moderiert von user profile iconTino: Quote-Tag hinzugefügt.


Popov - Mo 19.05.03 20:24

Keldorn hat folgendes geschrieben:
Hallo


Delphi-Quelltext
1:
procedure test(source :string;VAR SL: TstringList);                    


Das VAR kannst du dir bei Objekten sparen.

Es reicht also:


Delphi-Quelltext
1:
procedure test(source :string; SL: TstringList);                    


Bei Objekten wird nur der Zeiger übergeben und nicht der Wert.


Keldorn - Di 20.05.03 07:51

@popov. arrgh, immer diese kleinigkeiten. :wink: hast recht

Mfg Frank