Reputation: 65
I have the code below, but when I try to free the variable checkID, I get an access violation error, and if I don't destroy it I will have a memory leak problem.
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;
The smMethodsServerClient.CheckID(AID) method was created automatically through the TDSRestConnection component.
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;
I also used the Datasnap REST Client Module wizard to create my class TcmClientModule.
Upvotes: 2
Views: 1617
Reputation: 222
JSONValue used as a parameter of DataSnap does not need to be Free.
In addition, if you release the memory of the parameter object, an error may occur when you release the DataSnap DataModule or when you call the interface where the parameter was used for the second time.
Even if you create a new JSONValue parameter every time to use the DataSnap interface, there are no problems such as memory leaks.
Moreover, JSONValue objects received as a result of the DataSnap interface should not be freed further.
=================================================
clientModule.DisposeOf;
This frees memory for checkID. However, there is no setting for "checkID := nil". The conditional statement below will always be executed and an error occurs when executing.
if Assigned(checkId) then
checkID.Free;
Upvotes: 2
Reputation: 700
Maybe when you are doing this thing
clientModule.DisposeOf;
checkID
will be destroyed, because checkID
is a part of clientModule
due to this part of code
clientModule.smMethodsServerClient.CheckID(AID);
You can try clear checkID
first and then clear clientModule
.
Update: Another way to avoid error is checking checkId
before destroying.
Maybe this way is suitable:
if Assigned(checkId) then
checkID.Free;
Maybe besides this check you need check object for null
too.
Upvotes: 1