anchor.diff:      destructor Destroy;override;
     end;
   
  +//only here to work around compiler limitation.
  +function SafeMonitorTryEnter(const AObject: TObject): Boolean;
  +
  +const
  + SWeakReferenceError = 'TWeakReference can only be used with objects derived from TWeakReferencedObject';
  +
   
   implementation
   
   uses
  +  {$IFDEF USE_NS}
  +  System.TypInfo,
  +  System.Classes,
  +  System.Sysutils,
  +  System.SyncObjs;
  +  {$ELSE}
     TypInfo,
     classes,
  -  sysutils;
  -
  -{$IFDEF CPUX86}
  -
  -function InterlockedAdd(var Addend: Integer; Increment: Integer): Integer;
  +  SysUtils,
  +  SyncObjs;
  +  {$ENDIF}
  +
  +{$IFNDEF DELPHI_XE2_UP}
  +type
  +  TInterlocked = class
  +  public
  +    class function Increment(var Target: Integer): Integer; static; inline;
  +    class function Decrement(var Target: Integer): Integer; static; inline;
  +    class function Add(var Target: Integer; Increment: Integer): Integer;static;
  +    class function CompareExchange(var Target: Integer; Value, Comparand: Integer): Integer; static;
  +  end;
  +
  +class function TInterlocked.Decrement(var Target: Integer): Integer;
  +begin
  +  result := Add(Target,-1);
  +end;
  +
  +class function TInterlocked.Increment(var Target: Integer): Integer;
  +begin
  +  result := Add(Target,1);
  +end;
  +
  +class function TInterlocked.Add(var Target: Integer; Increment: Integer): Integer;
  +{$IFNDEF CPUX86}
   asm
  -      MOV   ECX,EAX
  -      MOV   EAX,EDX
  - LOCK XADD  [ECX],EAX
  -      ADD   EAX,EDX
  -end;
  -
  -
  -function InterlockedDecrement(var Addend: Integer): Integer;
  +  .NOFRAME
  +  MOV  EAX,EDX
  +  LOCK XADD [RCX].Integer,EAX
  +  ADD  EAX,EDX
  +end;
  +{$ELSE CPUX86}
   asm
  -      MOV   EDX,-1
  -      JMP   InterlockedAdd
  -end;
  -
  -function InterlockedIncrement(var Addend: Integer): Integer;
  +  MOV  ECX,EDX
  +  XCHG EAX,EDX
  +  LOCK XADD [EDX],EAX
  +  ADD  EAX,ECX
  +end;
  +{$ENDIF}
  +
  +class function TInterlocked.CompareExchange(var Target: Integer; Value, Comparand: Integer): Integer;
   asm
  -      MOV   EDX,1
  -      JMP   InterlockedAdd
  -end;
  -
  -
  -{$ELSE}
  -function InterlockedDecrement(var Addend: LongInt): LongInt;
  -asm
  -      .NOFRAME
  -      MOV   EAX,-1
  - LOCK XADD  [RCX].Integer,EAX
  -      DEC   EAX
  -end;
  -
  -function InterlockedIncrement(var Addend: LongInt): LongInt;
  -asm
  -      MOV   EAX,1
  - LOCK XADD  [RCX].Integer,EAX
  -      INC   EAX
  -end;
  -
  -
  -{$ENDIF}
  +  XCHG EAX,EDX
  +  XCHG EAX,ECX
  +  LOCK CMPXCHG [EDX],ECX
  +end;
  +{$ENDIF DELPHI_XE2_UPE2}
  +
  +//MonitorTryEnter doesn't do a nil check!
  +function SafeMonitorTryEnter(const AObject: TObject): Boolean;
  +begin
  +  if AObject <> nil then
  +    Result := TMonitor.TryEnter(AObject)
  +  else
  +    result := False;
  +end;
  +
   
   constructor TWeakReference<T>.Create(const data: T);
   var
  -  weakRef : IWeakReferenceableObject;
  -  d : IInterface;
  -begin
  -  d := IInterface(data);
  -  if Supports(d,IWeakReferenceableObject,weakRef) then
  -  begin
  -    FData := d as TObject;
  -    weakRef.AddWeakRef(@FData);
  +  target : IWeakReferenceableObject;
  +begin
  +  if data = nil then
  +    raise Exception.Create(format('[%s] passed to TWeakReference was nil', [PTypeInfo(TypeInfo(T)).Name]));
  +
  +  inherited Create;
  +
  +  if Supports(IInterface(data),IWeakReferenceableObject,target) then
  +  begin
  +    FData := IInterface(data) as TObject;
  +    target.AddWeakRef(@FData);
     end
     else
  -    raise Exception.Create('TWeakReference can only be used with objects derived from TWeakReferencedObject');
  +    raise Exception.Create(SWeakReferenceError);
   end;
   
   function TWeakReference<T>.Data: T;
  
positive.code: 
  destructor TWeakReference<T>.Destroy;
  var
    weakRef : IWeakReferenceableObject;
  begin
    if IsAlive then
    begin
      if Supports(FData,IWeakReferenceableObject,weakRef) then
      begin
        weakRef.RemoveWeakRef(@FData);
        weakRef := nil;
      end;
    end;
    FData := nil;
    inherited;
  end;
  
  
negative.code:   //The actual WeakReference implementation.
    TWeakReference<T: IInterface> = class(TInterfacedObject, IWeakReference<T>)
    private
      FData : Pointer;
    protected
      function IsAlive : boolean;
      function Data : T;
  