1

如何处理数据快照服务器中的对象引用?

我有返回 TUser 对象的 Datasnap 服务器方法

TServerMethods.CurrentUser(const AId: Integer): TUser
begin
  Result := GlobalCurrentUser; // <- Don't want to free this object
  Result.AddReference;
end;

TReferencedObject 子类中的 TUser

TReferencedObject = class(TObject)
private
  FReferenceCnt:     integer;
public
  procedure AddReference; virtual;
  procedure RemoveReference; virtual; // Frees object if reference count goes to 0
  property ReferenceCnt: integer Read FReferenceCnt Write FReferenceCnt;
end;

目前 DataSnap 服务器释放 TUser 对象,但我想为 TReferencedObject 后代调用 RemoveReference。

4

1 回答 1

1

我将 DataSnap 文件 Data.DBXJSONReflect.pas 和 Datasnap.DSReflect.pas 复制到项目目录,添加到项目并进行了三处修改。

Data.DBXJSONReflect 中的两个更改以设置正确的 ReferenceCount

1) TJSONUnMarshal.CreateObject

  if HasObject(ObjId) then
  begin
    Obj := GetObject(ObjId);
    if Obj is TReferencedObject then {Framework}
      TReferencedObject(Obj).AddReference; {Framework}    
    exit(Obj);
  end

2) TJSONPopulationCustomiser.PostPopulate

  if Data is TReferencedObject then {Framework}
    TReferencedObject(Data).AddReference; {Framework}

Datasnap.DSReflect 中的一项更改以发布参考

1) TDsMethodValues.ClearReferenceParameters

FUserObject.Clear更改为ClearUserObjects(FUserObjects);并添加了本地功能

  procedure ClearUserObjects(const AUserObjects: TObjectList<TObject>); {Framework}
  var
    i: Integer;
  begin
    FUserObjects.OwnsObjects := False;
    for i := FUserObjects.Count - 1 downto 0 do
      if FUserObjects.Items[i] is TReferencedObject then
      begin
        TReferencedObject(FUserObjects.Items[i]).RemoveReference;
        FUserObjects.Delete(i);
      end;
    FUserObjects.OwnsObjects := True;
    FUserObjects.Clear;
  end;
于 2012-05-21T12:35:51.593 回答