瀏覽代碼

* Do not allow same instance to be attached twice as observer

Michaël Van Canneyt 1 周之前
父節點
當前提交
940a86f869
共有 1 個文件被更改,包括 7 次插入4 次删除
  1. 7 4
      packages/fcl-base/src/fpobserver.pp

+ 7 - 4
packages/fcl-base/src/fpobserver.pp

@@ -390,9 +390,10 @@ implementation
 
 
 Resourcestring
-  SErrNotObserver = 'Instance of class %s is not an observer.';
-  SErrInvalidPropertyName = '%s is not a valid published property of class %s';
-  SErrObjectCannotBeObserved = 'Cannot observe an instance of class %d';
+  SErrNotObserver           = 'Instance of class %s is not an observer.';
+  SErrDuplicateObserver     = 'Cannot add an instance of class %s twice as observer';
+  SErrInvalidPropertyName   = '%s is not a valid published property of class %s';
+  SErrObjectCannotBeObserved = 'Cannot observe an instance of class %s';
   sErrInvalidFieldName      = 'No fieldname specified for column %d';
   sErrInvalidAlignmentChar  = 'Invalid alignment character "%s" specified for column %d';
   sErrInvalidWidthSpecifier = 'Invalid with "%s" specified for column %d';
@@ -492,7 +493,9 @@ begin
   If Not AObserver.GetInterface(SGUIDObserver,I) then
     Raise EObserver.CreateFmt(SErrNotObserver,[AObserver.ClassName]);
   If not Assigned(FObservers) then
-    FObservers:=TFPList.Create;
+    FObservers:=TFPList.Create
+  else if FObservers.IndexOf(aObserver)<>-1 then
+    Raise EObserver.CreateFmt(SErrDuplicateObserver,[AObserver.ClassName]);
   FObservers.Add(I);
 end;