1

Topic: Weak attribute

Something I was lost
If I correctly understood, [WEAK] variable are such special variable (objects) which are nullified in case of a call  somewhere. In this connection there is a separate manager of WEAK-references.
Soft links work either in NEXTGEN, or in the normal compiler, since Berlin.
What do I do not so?

type
TMyClass = class
public
[Weak] F: TObject;
end;
procedure TForm1.FormCreate (Sender: TObject);
var
Instance: TMyClass;
Obj: TObject;
begin
Instance: = TMyClass. Create;
try
Obj: = TObject. Create;
try
Instance. F: = Obj;
finally
Obj. Free;
end;
Caption: = Format (' 0x%p ', [Pointer (Instance. F)]);//here should be nil
finally
Instance. Free;
end;
end;

2

Re: Weak attribute

SOFT FOR YOU;
Weak works only in NextGen and from a normal object reference differs that does not change the counter of links.

3

Re: Weak attribute

SOFT FOR YOU;
In a berlin weak links work only with interfaces.

4

Re: Weak attribute

ziv-2014;
You are assured?
Because  the interface generally cannot be intercepted
To smooth out Weak-references

5

Re: Weak attribute

SOFT FOR YOU;
My especial judgement, purely theoretically-hypothetically. While local variable Obj is live, there will live also the address on it.
Hence, delete in one procedure, and check in another when obj it will be already destroyed
It after all links so cannot will be deleted in point For now is in

6

Re: Weak attribute

SOFT FOR YOU;
It is assured. Anybody  does not intercept. It is made through magic procedures.

7

Re: Weak attribute

SOFT FOR YOU;
And they weak do not smooth out the link. At the link to a variable with a weak reference, the counter does not increase.
And the pointer can remain not cleared.

8

Re: Weak attribute

I do not understand

type
TMyClass = class
public
[Weak] I: IInterface;
end;
procedure TForm1.Button1Click (Sender: TObject);
var
Context: TRttiContext;
RttiType: TRttiType;
Fields: TArray <TRttiField>;
Field: TRttiField;
Attributes: TArray <TCustomAttribute>;
begin
Context: = TRttiContext. Create;
RttiType: = Context. GetType (TMyClass);
Fields: = RttiType. GetFields;
Field: = Fields [0];
Attributes: = Field. GetAttributes;
Caption: = Field. Name + ' attributes: ' + IntToStr (Length (Attributes));
end;

Attributes are not present

9

Re: Weak attribute

SOFT FOR YOU;
Because it is necessary so:

{$M +}
TMyClass = class
public
[Weak] I: IInterface;
end;
{$M-}

10

Re: Weak attribute

Let's correlate our knowledge of Weak-references
Judging by System. _ FinalizeRecord, under Weak to the logician 3 types fall:
1. TObject
-  WEAK INST REF
- Assignment InstWeakCopy-> RegisterWeakRef-> to add the reference from a table hash
- Cleaning InstWeakClear-> UnregisterWeakRef-> to delete the link from a table hash
- In  TObject/TInterfacedObject-> CleanupInstance-> TInstHashMap. Destroy-> TInstItem. ClearWeakRefs-> to nullify all fields connected to the link
2. function of object
-  WEAK INST REF
- Assignment CopyClosure-> RegisterWeakMethodRef
- Cleaning ClosureRemoveWeakRef-> ClosureRemoveWeakRef
3. IInterface
-  WEAK INTF REF
- Assignment-> InstWeakCopy-> RegisterWeakRef
- Cleaning-> InstWeakClear-> UnregisterWeakRef
It turns out, soft links is not simply variable which are initialized by the normal pointer. It is the difficult pointer which is nullified at  and original object.