Einzelnen Beitrag anzeigen

Benutzerbild von Jens Schumann
Jens Schumann

Registriert seit: 27. Apr 2003
Ort: Bad Honnef
1.644 Beiträge
 
Delphi 2009 Professional
 
#64

Re: Neuer Artikel bei mir: %HOMEDIR% - das unbekannte Verzei

  Alt 28. Sep 2005, 09:27
Hallo Luckie,
über diese Diskussion bin ich hier hergekommen.
Bei der function GetShellFolder ist mir folgendes aufgefallen:
Delphi-Quellcode:
function GetShellFolder(CSIDL: integer): string;
var
  pidl : PItemIdList;
  FolderPath : string;
  SystemFolder : Integer;
begin
  SystemFolder := CSIDL;
  if SUCCEEDED(SHGetSpecialFolderLocation(0, SystemFolder, pidl)) then
  begin
    SetLength(FolderPath, max_path);
    if SHGetPathFromIDList(pidl, PChar(FolderPath)) then
    begin
      SetLength(FolderPath, length(PChar(FolderPath)));
    end;
  end;
  Result := FolderPath; // <- Welchen Wert hat FolderPath wenn
                        // SUCCEEDED einen Fehler zurückgibt ???

  // Hier wird pidl nicht freigeben -> Funktion erzeugt Speicherleck

end;
Zitat von MSDN:
ppidl
[out] A pointer to an item identifier list (PIDL) specifying the folder's location relative to the root of the namespace (the desktop). The calling application is responsible for freeing this pointer with the Shell's IMalloc interface (see SHGetMalloc).
Etwas besser wäre es so:
Delphi-Quellcode:
function GetShellFolder(CSIDL: integer): string;
var
  pidl : PItemIdList;
  FolderPath : string;
  SystemFolder : Integer;
  Malloc : IMalloc; // Interface für IMalloc
begin
  Malloc:=Nil;
  FolderPath:=''; // Initialisieren damit Result immer definiert ist
  SHGetMalloc(Malloc); // Interface holen damit am Ende pidl freigegeben werden kann
  If Malloc=Nil then
    begin
    Result:=FolderPath;
    Exit;
    end;
  Try
    SystemFolder := CSIDL;
    if SUCCEEDED(SHGetSpecialFolderLocation(0, SystemFolder, pidl)) then
    begin
      SetLength(FolderPath, max_path);
      if SHGetPathFromIDList(pidl, PChar(FolderPath)) then
      begin
        SetLength(FolderPath, length(PChar(FolderPath)));
      end;
    end;
    Result := FolderPath;
  Finally
    Malloc.Free(pidl); // Speicher freigeben
    end;
end;
Ich schreibe Artikel oder Tutorials wenn 100%tig weiss was ich schreibe. Deshalb habe ich noch nie einen Artikel oder ein Tutorial geschrieben.
I come from outer space to save the human race
  Mit Zitat antworten Zitat