anchor.diff:      Result := E_NOINTERFACE;
   end;
   
  -
   function TWeakReferencedObject._AddRef: Integer;
   begin
  -  Result := InterlockedIncrement(FRefCount);
  +{$IFNDEF AUTOREFCOUNT}
  +  Result := TInterlocked.Increment(FRefCount);
  +{$ELSE}
  +  Result := __ObjAddRef;
  +{$ENDIF}
   end;
   
   function TWeakReferencedObject._Release: Integer;
  -begin
  -  Result := InterlockedDecrement(FRefCount);
  -  if Result = 0  then
  +
  +{$IFNDEF AUTOREFCOUNT}
  +  procedure __MarkDestroying(const Obj);
  +  var
  +    LRef: Integer;
  +  begin
  +    repeat
  +      LRef := TWeakReferencedObject(Obj).FRefCount;
  +    until TInterlocked.CompareExchange(TWeakReferencedObject(Obj).FRefCount, LRef or objDestroyingFlag, LRef) = LRef;
  +  end;
  +{$ENDIF}
  +
  +begin
  +{$IFNDEF AUTOREFCOUNT}
  +  Result := TInterlocked.Decrement(FRefCount);
  +  if Result = 0 then
  +  begin
  +    __MarkDestroying(Self);
       Destroy;
  -end;
  -
  +  end;
  +{$ELSE}
  +  Result := __ObjRelease;
  +{$ENDIF}
  +end;
   
   end.
  
positive.code: { TWeakReferencedObject }
  
  procedure TWeakReferencedObject.AddWeakRef(value: Pointer);
  var
    l : integer;
  begin
    MonitorEnter(Self);
    try
      l := Length(FWeakReferences);
      Inc(l);
      SetLength(FWeakReferences,l);
      FWeakReferences[l-1] := Value;
    finally
      MonitorExit(Self);
    end;
  end;
  
  procedure TWeakReferencedObject.RemoveWeakRef(value: Pointer);
  var
    l : integer;
    i : integer;
    idx : integer;
  begin
    MonitorEnter(Self);
    try
      l := Length(FWeakReferences);
      if l > 0 then
      begin
        idx := -1;
        for i := 0 to l - 1 do
        begin
          if idx <> -1 then
          begin
            FWeakReferences[i -1] := FWeakReferences[i];
          end;
          if FWeakReferences[i] = Value then
          begin
            FWeakReferences[i] := nil;
            idx := i;
          end;
        end;
        Dec(l);
        SetLength(FWeakReferences,l);
      end;
    finally
      MonitorExit(Self);
    end;
  end;
  
  
  procedure TWeakReferencedObject.AfterConstruction;
  begin
    // Release the constructor's implicit refcount
    InterlockedDecrement(FRefCount);
  end;
  
  procedure TWeakReferencedObject.BeforeDestruction;
  var
    i: Integer;
  begin
    if RefCount <> 0 then
      System.Error(reInvalidPtr);
    MonitorEnter(Self);
    try
      for i := 0 to Length(FWeakReferences) - 1 do
         TObject(FWeakReferences[i]^) := nil;
      SetLength(FWeakReferences,0);
    finally
      MonitorExit(Self);
    end;
  end;
  
  function TWeakReferencedObject.GetRefCount: integer;
  begin
    result := FRefCount;
  end;
  
  class function TWeakReferencedObject.NewInstance: TObject;
  begin
    // Set an implicit refcount so that refcounting
    // during construction won't destroy the object.
    Result := inherited NewInstance;
    TWeakReferencedObject(Result).FRefCount := 1;
  end;
  
  function TWeakReferencedObject.QueryInterface(const IID: TGUID; out Obj): HResult;
  
negative.code: 
  unit Delphi.Mocks.WeakReference;
  
  
  interface
  
  {$I 'Delphi.Mocks.inc'}
  uses
    {$IFDEF USE_NS}
    System.Generics.Collections;
    {$ELSE}
    Generics.Collections;
    {$ENDIF}
  
  type
    /// Implemented by our weak referenced object base class
    IWeakReferenceableObject = interface
      ['{3D7F9CB5-27F2-41BF-8C5F-F6195C578755}']
      procedure AddWeakRef(value : Pointer);
      procedure RemoveWeakRef(value : Pointer);
      function GetRefCount : integer;
    end;
  
    ///  This is our base class for any object that can have a weak reference to
    ///  it. It implements IInterface so the object can also be used just like
    ///  any normal reference counted objects in Delphi.
    TWeakReferencedObject = class(TObject, IInterface, IWeakReferenceableObject)
    private const
      objDestroyingFlag = Integer($80000000);
    protected
    {$IFNDEF AUTOREFCOUNT}
      FRefCount: Integer;
    {$ENDIF}
      FWeakReferences : TList<Pointer>;
      function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
      function _AddRef: Integer; virtual; stdcall;
      function _Release: Integer; virtual;stdcall;
      procedure AddWeakRef(value : Pointer);
      procedure RemoveWeakRef(value : Pointer);
      function GetRefCount : integer; inline;
    public
      procedure AfterConstruction; override;
      procedure BeforeDestruction; override;
      {$IFDEF NEXTGEN}[Result: Unsafe]{$ENDIF} class function NewInstance: TObject; override;
    {$IFNDEF AUTOREFCOUNT}
      property RefCount: Integer read GetRefCount;
    {$ENDIF}
    end;
  
    // This is our generic WeakReference interface
    IWeakReference<T : IInterface> = interface
      function IsAlive : boolean;
      function Data : T;
    end;
  