Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   PSafeArray, Speicher steigt an (https://www.delphipraxis.net/173135-psafearray-speicher-steigt.html)

thatsit 8. Feb 2013 16:21

Delphi-Version: 5

PSafeArray, Speicher steigt an
 
Hallo,

ich bräuchte mal Hilfe. Ich habe ein Programm geschrieben, wozu ich auch quelltext Beispiele im Netz gefunden hab usw. Folgendes Problem, es geht darum, dass der Inhalt einer Webseite nach bestimmten sachen wie Links oder Bildern durchsucht werden soll. Es klappt auch alles, nur steigt der Speicher meines Programmes permanent an umso öfter man die Funktion verwendet. Der Speicher scheint nicht wieder freigeben zu werden. Überall ist zu lesen man muss das SafeArray zerstören sonst bleiben die Ole Variants im Speicher und der steigt immer weiter an. Hab schon vieles versucht, VarClear, SafeArrayDestroy usw. Aber es ändert nichts, Speicher steigt bei erneutem Funktions durchlauf immer weiter. Nur Programm schließen hilft...

Code ist ähnlich diesem:

Code:
uses mshtml, ActiveX, COMObj, IdHTTP, idURI;

{ .... }

procedure GetImageLinks(AURL: string; AList: TStrings);
var
  IDoc: IHTMLDocument2;
  strHTML: string;
  v: Variant;
  x: Integer;
  ovLinks: OleVariant;
  DocURL: string;
  URI: TidURI;
  ImgURL: string;
  idHTTP: TidHTTP;
begin
  AList.Clear;
  URI := TidURI.Create(AURL);
  try
    DocURL := 'http://' + URI.Host;
    if URI.Path <> '/' then
      DocURL := DocURL + URI.Path;
  finally
    URI.Free;
  end;
  Idoc := CreateComObject(Class_HTMLDocument) as IHTMLDocument2;
  try
    IDoc.designMode := 'on';
    while IDoc.readyState <> 'complete' do
      Application.ProcessMessages;
    v     := VarArrayCreate([0, 0], VarVariant);
    idHTTP := TidHTTP.Create(nil);
    try
      strHTML := idHTTP.Get(AURL);
    finally
      idHTTP.Free;
    end;
    v[0] := strHTML;
    IDoc.Write(PSafeArray(System.TVarData(v).VArray));
    IDoc.designMode := 'off';
    while IDoc.readyState <> 'complete' do
      Application.ProcessMessages;
    ovLinks := IDoc.all.tags('IMG');
    if ovLinks.Length > 0 then
    begin
      for x := 0 to ovLinks.Length - 1 do
      begin
        ImgURL := ovLinks.Item(x).src;
        // The stuff below will probably need a little tweaking
        // Deteriming and turning realtive URLs into absolute URLs
        // is not that difficult but this is all I could come up with
        // in such a short notice.
        if (ImgURL[1] = '/') then
        begin
          // more than likely a relative URL so
          // append the DocURL
          ImgURL := DocURL + ImgUrl;
        end
        else
        begin
          if (Copy(ImgURL, 1, 11) = 'about:blank') then
          begin
            ImgURL := DocURL + Copy(ImgUrl, 12, Length(ImgURL));
          end;
        end;
        AList.Add(ImgURL);
      end;
    end;
  finally
    IDoc := nil;
  end;
end;
ich bin der Meinung der Teufel liegt hier
Zitat:

IDoc.Write(PSafeArray(System.TVarData(v).VArray));
Ich hab es auch schon mit einem extra angelegten SafeArray versucht, dann hinter her mit destroy. aber funktioniert irgendwie nicht. Das hat ja was mit Pointern zu tun. Wer schön wenn jemand ne Idee hätte, wie ich den Speicher wieder freibekomm oder es irgendwie über ne andere Array funktion realisiere.

Cheers

Bernhard Geyer 8. Feb 2013 16:32

AW: PSafeArray, Speicher steigt an
 
Ach der liebe IE.

Der ist mistig Implementiert hat und Speicherlücken wenn man seine Interface entsprechenden den Vorgaben verwendet.
Probier einfach mal bei diversen Interfaces mit Auruf der _Release-Methode am Ende der Verwendung die internen Fehler des IE zu umgehen.
Aber alles schön mit try...except sichern damit dein Programm nicht kracht wenn die Fehler korrigiert werden.

sx2008 8. Feb 2013 18:00

AW: PSafeArray, Speicher steigt an
 
Zitat:

Zitat von MSDN (Microsoft)
Note When document.write or document.writeln is used in an event handler, you must also use document.close.

Keine Ahnung weshalb das close nur in einem Event Handler aufgerufen werden soll, aber ich würde das mal versuchen.

thatsit 20. Feb 2013 20:39

AW: PSafeArray, Speicher steigt an
 
bis jetz konnt ich noch kein erfolg verbuchen... habs mal mit twebbrowser nun probiert, dort wird der speicher nach jeder anfrage resettet so wie es sein sollte...


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:35 Uhr.

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