Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi TStringList initialisieren! Wie? (https://www.delphipraxis.net/133466-tstringlist-initialisieren-wie.html)

Deltachaos 2. Mai 2009 12:38


TStringList initialisieren! Wie?
 
Ich bekomme vom Compiler die Warnung:

Code:
[Warnung] Unit1.pas(254): Variable 'pub' ist möglicherweise nicht initialisiert worden
Es funktionirt ja auch so nur ich möchte eigentlich keine Warnungen haben.
Wie initialisiere ich den eine TStringList?
Geht das nicht mit
Delphi-Quellcode:
pub := TStringList.Create;
?


Delphi-Quellcode:
procedure delphi_versionadd(ort, key: string);
var regist: TRegistry;
    pub: TStringList;
    pubdir, pubbrc32, pubbrcc32: string;
    pubi: integer;
begin
  regist := TRegistry.Create;
  try
    regist.RootKey := HKEY_LOCAL_MACHINE;
    pub := TStringList.Create;
    regist.OpenKey(ort, false);
    regist.GetKeyNames(pub);
    regist.CloseKey;
    if pub.count > 0 then
    begin
      for pubi := 0 to pub.count - 1 do
      begin
        regist.OpenKeyReadOnly(ort + '\' + pub[pubi]);
        if regist.ValueExists(key) then
        begin
          pubdir := regist.ReadString(key);
          regist.CloseKey;
          pubdir := IncludeTrailingPathDelimiter(pubdir) + 'Bin\';
          if FileExists(pubdir + 'brc32.exe') then
            pubbrc32 := pubdir + 'brc32.exe';
          if FileExists(pubdir + 'brcc32.exe') then
            pubbrcc32 := pubdir + 'brcc32.exe';
          additem(pub[pubi], pubbrc32, pubbrcc32);
        end;
      end;
    end;
  finally
    regist.free;
    pub.Free;
  end;
end;

Luckie 2. Mai 2009 12:39

Re: TStringList initialisieren! Wie?
 
Erzeug die StringListe ausserhalb des try-finally-Blocks.

Chemiker 2. Mai 2009 12:46

Re: TStringList initialisieren! Wie?
 
Hallo Deltachaos,


Delphi-Quellcode:
finally
  regist.free;
  pub.Free;
end;
besser:

Delphi-Quellcode:
finally
  pub.Free;
  regist.free;
end;
FIVO-Prinzip!

Bis bald Chemiker

himitsu 2. Mai 2009 12:49

Re: TStringList initialisieren! Wie?
 
wie Luckie schon sagte:
Delphi-Quellcode:
regist := TRegistry.Create;
try
  pub := TStringList.Create;
  try
    ...
  finally
    pub.Free;
  end;
finally
  regist.free;
end;
oder notfalls auch sowas:
Delphi-Quellcode:
pub := nil;
regist := TRegistry.Create;
try
  ...
  pub := TStringList.Create;
  ...
finally
  {if Assigned(pub) then} pub.Free;
  regist.free;
end;
[edit]
ach stimmt ja ... nja, zumindestens wenn man mal etwas nimmt, was nicht auf NIL prüft, dann halt selber prüfen

Apollonius 2. Mai 2009 12:51

Re: TStringList initialisieren! Wie?
 
Free prüft doch schon auf nil, insofern kannst du dir das Assigned sparen.

mjustin 2. Mai 2009 13:42

Re: TStringList initialisieren! Wie?
 
Zitat:

Zitat von Apollonius
Free prüft doch schon auf nil, insofern kannst du dir das Assigned sparen.

Die Exception könnte schon vor der Erzeugung aufgetreten sein. Dann wäre Pub im finally noch nil.

Apollonius 2. Mai 2009 13:46

Re: TStringList initialisieren! Wie?
 
Schlag mal TObject.Free in System.pas auf. :wink:

himitsu 2. Mai 2009 13:54

Re: TStringList initialisieren! Wie?
 
Zitat:

Zitat von mjustin
Die Exception könnte schon vor der Erzeugung aufgetreten sein. Dann wäre Pub im finally noch nil.

drumm muß es vor dem Try-Finally auf NIL gesetzt (also initialisiert) werden.
Dabei ist es jetzt egal ob selbst mit Assigned geprüft wird oder ob es .Free selber nochmal macht ... wenn der Objektteiger dort sonstwohin zeigt, würde es (nochmal) knallen oder schlimmer noch, er zeigt zufällig auf ein anderes existierendes Object, dann würdes dieses freigegeben und es knallt plötzlich anderswo und man weiß nicht warum.

Deltachaos 2. Mai 2009 15:05

Re: TStringList initialisieren! Wie?
 
danke.
Ich hab das

Delphi-Quellcode:
pub := TStringList.Create;
nun vor

Delphi-Quellcode:
try
geschrieben

Delphi-Quellcode:
procedure delphi_versionadd(ort, key: string);
var regist: TRegistry;
    pub: TStringList;
    pubdir, pubbrc32, pubbrcc32: string;
    pubi: integer;
begin
  regist := TRegistry.Create;
  pub := TStringList.Create;
  try
    regist.RootKey := HKEY_LOCAL_MACHINE;
    regist.OpenKey(ort, false);
    regist.GetKeyNames(pub);
    regist.CloseKey;
    if pub.count > 0 then
    begin
      for pubi := 0 to pub.count - 1 do
      begin
        regist.OpenKeyReadOnly(ort + '\' + pub[pubi]);
        if regist.ValueExists(key) then
        begin
          pubdir := regist.ReadString(key);
          regist.CloseKey;
          pubdir := IncludeTrailingPathDelimiter(pubdir) + 'Bin\';
          if FileExists(pubdir + 'brc32.exe') then
            pubbrc32 := pubdir + 'brc32.exe';
          if FileExists(pubdir + 'brcc32.exe') then
            pubbrcc32 := pubdir + 'brcc32.exe';
          additem(pub[pubi], pubbrc32, pubbrcc32);
        end;
      end;
    end;
  finally
    regist.free;
  end;
end;

SirThornberry 2. Mai 2009 15:24

Re: TStringList initialisieren! Wie?
 
wo ist jetzt aber dein pub.free?
Korrekt wäre es (wie bereits erwähnt) so:
Delphi-Quellcode:
object1 := TObject1.Create();
try
  object2 := TObject2.Create();
  try
    //do something with object2 and object1
  finally
    object2.Free;
  end;
finally
  object1.Free;
end;
von folgendem würde ich abraten:
Delphi-Quellcode:
object1 := TObject1.Create();
object2 := TObject2.Create();
try
  //do something with object2 and object1
finally
  object1.Free;
  object2.Free;
end;
Denn wenn bei TObject2.Create() ein Fehler auftritt wird Object1 nicht frei gegeben obwohl da beim instanzieren kein Fehler aufgetreten ist.


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:09 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz