Autor Beitrag
Aiko07
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Sa 17.05.03 14:36 
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
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:
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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: 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

ausblenden 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

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)
Aiko07 Threadstarter
Hält's aus hier
Beiträge: 9



BeitragVerfasst: 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
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: 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:
ausblenden 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;


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


Moderiert von user profile iconTino: Quote-Tag hinzugefügt.
Popov
ontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic star
Beiträge: 1655
Erhaltene Danke: 13

WinXP Prof.
Bei Kleinigkeiten D3Pro, bei größeren Sachen D6Pro oder D7
BeitragVerfasst: Mo 19.05.03 20:24 
Keldorn hat folgendes geschrieben:
Hallo

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


Das VAR kannst du dir bei Objekten sparen.

Es reicht also:

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


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

_________________
Popov
Keldorn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 2266
Erhaltene Danke: 4

Vista
D6 Prof, D 2005 Pro, D2007 Pro, DelphiXE2 Pro
BeitragVerfasst: Di 20.05.03 07:51 
@popov. arrgh, immer diese kleinigkeiten. :wink: hast recht

Mfg Frank

_________________
Lükes Grundlage der Programmierung: Es wird nicht funktionieren.
(Murphy)