[英]Access violation when i try free TJSONObject
我有下面的代碼,但是當我嘗試釋放變量 checkID 時,出現訪問沖突錯誤,如果我不銷毀它,我將遇到 memory 泄漏問題。
function TdtmData.CheckID(AID: String): Boolean;
var
checkID : TJSONObject;
clientModule : TcmClientModule;
ok : Boolean;
begin
Result := False;
try
try
clientModule := TcmClientModule.Create(Self);
checkID := clientModule.smMethodsServerClient.CheckID(AID);
ok := checkID.GetValue<Boolean>('Register', False);
if not(ok) then
raise Exception.Create('ID ERROR.');
finally
clientModule.DisposeOf;
checkID.Free; // <-- The error is here (Access violation)
end;
Result := ok;
except
on e : Exception do
raise Exception.Create(e.Message);
end;
end;
smMethodsServerClient.CheckID(AID) 方法是通過 TDSRestConnection 組件自動創建的。
function TsmMethodsServerClient.CheckID(AID: string; const ARequestFilter: string): TJSONObject;
begin
if FCheckIDCommand = nil then
begin
FCheckIDCommand := FConnection.CreateCommand;
FCheckIDCommand.RequestType := 'GET';
FCheckIDCommand.Text := 'TsmMethodsServer.CheckID';
FCheckIDCommand.Prepare(TsmMethodsServer_CheckID);
end;
FCheckIDCommand.Parameters[0].Value.SetWideString(AIDPDV);
FCheckIDCommand.Execute(ARequestFilter);
Result := TJSONObject(FCheckIDCommand.Parameters[1].Value.GetJSONValue(FInstanceOwner));
end;
我還使用 Datasnap REST 客戶端模塊向導來創建我的 class TcmClientModule。
用作 DataSnap 的參數的 JSONValue 不需要是 Free。
另外,如果釋放參數object的memory,釋放DataSnap DataModule或第二次調用該參數的接口時可能會出錯。
即使每次都創建一個新的 JSONValue 參數來使用 DataSnap 接口,也不會出現 memory 泄漏等問題。
此外,不應進一步釋放作為 DataSnap 接口結果接收的 JSONValue 對象。
=================================================
clientModule.DisposeOf;
這為 checkID 釋放了 memory。 但是,“checkID:= nil”沒有設置。 下面的條件語句會一直執行,執行時會出錯。
if Assigned(checkId) then
checkID.Free;
也許當你在做這件事時
clientModule.DisposeOf;
checkID
將被銷毀,因為checkID
是clientModule
的一部分,因為這部分代碼
clientModule.smMethodsServerClient.CheckID(AID);
您可以先嘗試清除checkID
,然后再清除clientModule
。
更新:避免錯誤的另一種方法是在銷毀之前檢查checkId
。 也許這種方式是合適的:
if Assigned(checkId) then
checkID.Free;
也許除了這個檢查之外,您還需要檢查 object 的null
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.