2011-08-29 51 views
0

我通過SAP Logon Control TLB(首次啓動時消耗大約25MB的內存!!)連接到SAP服務器,然後查詢一些數據。每個呼叫需要〜200 kB。因爲我不想每次都重新連接,所以每次需要時我都會存儲連接並將其傳遞給SAP函數對象(似乎該對象被複制,因爲此proc的成本大約爲6MB)。在完成查詢後,我釋放了該對象......但內存使用情況並未下降?因此,如果我讓程序運行了大約4個小時,我的內存已滿,電腦崩潰。Delphi/SAP函數OCX /登錄控制:內存泄漏

代碼(簡化):

connection.pas(創建連接):

SAPLogonCtrl : TSAPLogonControl; 

constructor TCon.Create(usr, pswd, sys, appserv, sysnum, clnt); 
begin 
    inherited Create; 
    SAPLogonCtrl := TSAPLogonControl.Create(nil); 
    with SAPLogonCtrl do begin 
     User := usr; 
     Password := pswd; 
     ... 
     Client := clnt; 
    end; 
    FConnection := SAPLogonCtrl.NewConnection; 
    FConnection.Logon(0, true); //<------------- this needs ~25MB 
end; 

main.pas:

... 
procedure TMain.Query; 
var 
    theQuery : TSomeQuery; 
begin 
    theQuery := TSomeQuery.Create; 
    theQuery.Data1 := 'something gets here'; 
    theQuery.Data2 := 'here too'; 
    theQuery.Call; // <------------------------ this needs about ~100kB 
    ... 
    theQuery.Free; // <------------------------ nothing happens here, no mem freed! 
end; 
... 

someQuery.pas(創建對象和呼叫查詢):

var 
    mySAPFunction: TSapFunctions; 
    mySAPQuery: Variant; 

... 
procedure Call; 
begin 
    mySAPFunction := TSAPFunctions.Create; 
    mySAPFunction.Connection := FConnection; // <---- connection is passed (copied? costs about 5MB) from connection.pas 
    mySAPFunction.RemoveAll; // removes prevous added interfaces 
    mySAPQuery := mySAPFunction.Add('interface here'); 
    mySAPQuery.Call; 
    ... 
    // return the result 
end; 

我希望這是可以理解的,有人可以幫助我,因爲與此內存泄漏我的程序實際上不可用:(

在此先感謝, Eike。

+0

東西是不正確的......你定義「myQuery:Variant」;然後使用「mySAPQuery」 – ComputerSaysNo

+0

謝謝,編輯 –

回答

2

您可以強制通過將其設定釋放變種接口實例爲nil:

procedure Call; 
begin 
    mySAPFunction := TSAPFunctions.Create; 
    mySAPFunction.Connection := FConnection; // <---- connection is passed (copied? costs about 5MB) from connection.pas 
    mySAPFunction.RemoveAll; // removes prevous added interfaces 
    mySAPQuery := mySAPFunction.Add('interface here'); 
    mySAPQuery.Call; 
    mySAPQuery := null; // will release the memory 
end; 

其實,我覺得mySAPQuery應您的本地通話過程:在這種情況下,mySapQuery := null說明會由編譯器製作。

+0

將它設置爲零會產生一個錯誤:不兼容的類型:'Variant'和'指針'。你的意思是來自變體單元的空值? –

+0

是的。我在SO中寫了這個。並在IInterface和Variant之間混淆了一下。 ;) –