Einzelnen Beitrag anzeigen

LeoDD

Registriert seit: 30. Jul 2003
43 Beiträge
 
Delphi 2010 Professional
 
#7

Re: Speicherleck bei verschachtelten Objekten

  Alt 15. Jan 2009, 08:35
Hier mal der Source von TTC7Encyclopedia:

Delphi-Quellcode:
{
  Klasse für die Arbeit mit der Enzyklopädie
}

unit tc7_class_encyclopedia;

interface

uses
  Classes, SysUtils, xmldom, XMLDoc, XMLIntf, Dialogs, Contnrs,

  //eigene Units
  tc7_functions_common,
  xmlcomm_class_encyclopedia;


type

  TTC7EncyclopediaDataStruc = record

    //die ID, die als Haupt-Prüfkriterium gilt
    PrimaryID: string;
    //weitere IDs, für die dieser Eintrag gilt (kommaseparierte Liste)
    SecondIDList: string;
    //Titel des Eintrages
    Caption: string;
    //Teaser, kurze Beschreibung
    Teaser: string;
    //die Beschreibung des Eintrages
    Description: string;
    //Datum der Erfassung des Eintrages
    CreateDate: string;
    //Gefahrenlevel (0=grün, 1=unbekannt, 2=rot)
    DangerLevel: byte;
    //Kann gelöscht werden? Oder evtl. für Betrieb nötig?
    //(0=ja, 1=nachfragen, 2=nein)
    CanRemoved: byte;
    //Dateiname des Eintrages (XML Datei, woraus der Eintrag stammt)
    EncyclopediaFile: string;

  end;

  PTTC7EncyclopediaDataStruc = ^TTC7EncyclopediaDataStruc;

  TTC7Encyclopedia = class;


  {
    class TC7Encyclopedia
  }

  TTC7Encyclopedia = class(TObjectList)

    public
      constructor Create;

      function ReadEncyclopedia(EncyclopediaDirectory: string): integer;
      function GetInfo(UniqueID: shortstring; var InfoStruc: TTC7EncyclopediaDataStruc): boolean;

    private

    protected

  end;


implementation


constructor TTC7Encyclopedia.Create;
begin

  inherited Create;

end;


{
  Liest die Enzyklopädie von der Festplatte in die Liste.

  Gibt -1 zurück, wenn ein Fehler auftrat, ansonsten die Anzahl der Einträge
}

function TTC7Encyclopedia.ReadEncyclopedia(EncyclopediaDirectory: string): integer;
var
  SRec: TSearchRec;
  OK: integer;
  EncyclopediaEntry: IXMLTc7_encyclopediaType;
  EncyclopediaData: TTC7EncyclopediaDataStruc;
  PEncyclopediaData: PTTC7EncyclopediaDataStruc;
  i: integer;
  SecondaryList: TStringList;
begin

  Result := -1;

  //Einlesen der XML Dateien
  if DirectoryExists(EncyclopediaDirectory) then begin

    Result := 0;

    SecondaryList := TStringList.Create;

    OK := FindFirst(EncyclopediaDirectory + '*.xml', faAnyFile, SRec);
    while OK=0 do begin

      try
        EncyclopediaEntry := Loadtc7_encyclopedia(EncyclopediaDirectory + SRec.Name);
        with EncyclopediaData do begin
          PrimaryID := EncyclopediaEntry.Identification.Primary;
          SecondIDList := '';
          if EncyclopediaEntry.Identification.Secondary.Count>0 then begin
            for i:=0 to EncyclopediaEntry.Identification.Secondary.Count-1 do begin
              SecondaryList.Add(EncyclopediaEntry.Identification.Secondary.Id[i]);
            end;
            SecondIDList := SecondaryList.CommaText;
          end;
          Caption := StringRemoveTabs(Trim(EncyclopediaEntry.Caption));
          Teaser := StringRemoveTabs(Trim(EncyclopediaEntry.Teaser));
          Description := StringRemoveTabs(Trim(EncyclopediaEntry.Description));
          CreateDate := StringRemoveTabs(Trim(EncyclopediaEntry.CreateDate));
          DangerLevel := EncyclopediaEntry.Dangerlevel;
          CanRemoved := EncyclopediaEntry.Canremoved;
          EncyclopediaFile := SRec.Name;
        end;
        New(PEncyclopediaData);
        PEncyclopediaData^ := EncyclopediaData;
        
        Add(TObject(PEncyclopediaData));

        Inc(Result);
      except
        ShowMessage('Invalid encyclopedia file: "' + SRec.Name + '"');
      end;

      OK := FindNext(SRec);
    end;
    FindClose(SRec);

    SecondaryList.Free;

  end;
end;



{
  Holen der Info anhand einer UniqueID.
  Wird die ID gefunden, werden die Daten in der Struktur gespeichert und TRUE zurückgegeben.
  Wird die ID nicht gefunden, wird FALSE zurückgegeben.
}

function TTC7Encyclopedia.GetInfo(UniqueID: shortstring; var InfoStruc: TTC7EncyclopediaDataStruc): boolean;
var
  i: integer;
  Pointer: PTTC7EncyclopediaDataStruc;
  SecondaryList: TStringList;
begin

  Result := false;

  if Count>0 then begin
    //PrimaryIDs durchsuchen
    for i:=0 to Count-1 do begin
      Pointer := PTTC7EncyclopediaDataStruc(Items[i]);
      if UniqueID = Pointer^.PrimaryID then begin
        InfoStruc := Pointer^;
        Result := true;
      end;
    end;
    //SecondaryIDs durchsuchen
    if not Result then begin
      SecondaryList := TStringList.Create;
      for i:=0 to Count-1 do begin
        Pointer := PTTC7EncyclopediaDataStruc(Items[i]);
        SecondaryList.CommaText := Pointer^.SecondIDList;
        if SecondaryList.IndexOf(UniqueID)>-1 then begin
          InfoStruc := Pointer^;
          Result := true;
        end;
      end;
      SecondaryList.Free;
    end;
  end;

end;



{
  PRIVATE
}



end.
Hab ich irgendwo was übersehen oder sollte ich etwas anders lösen?
Erst wenn man dreimal auf Holz klopfen will, stellt man fest, dass die Welt nur noch aus Plastik und Aluminium besteht.
  Mit Zitat antworten Zitat