Weak references problem

yc81reja
Erstie
Erstie
Beiträge: 12
Registriert: 18. Apr 2015 16:25

Weak references problem

Beitrag von yc81reja »

Hi all, I got one last problem by assignment 7 to implement tryderef. Since I tried some search on google without luck, I personally assume that tryderef will return the interpretation of e if the its weak reference is valid, otherwise interpretation of default will be returned. Correct me about this assumption if I'm wrong.
Then the problem comes. The last test case which first allocates all memory for the weak refernce of 'ref then defined another 'x. Since there're only two slots of memory, 'x will replace all slots with its refernce and value. However the 'ref still remains in stack which is mapped to the location of current 'x. The stack is immutable, thus it's impossible to change anything in stack from gc. I'm wondering if there's any workaround for this. Thanks

IvaToteva
Neuling
Neuling
Beiträge: 10
Registriert: 15. Apr 2015 16:41

Re: Weak references problem

Beitrag von IvaToteva »

Hi,

De-referencing is the inverse operation of referencing. When you try to de-reference a reference, it can happen that it is a valid reference to a location in the store - then you return the value in the store. It is also possible that the weak reference has already been invalidated - then the result of the default expression of TryDeref is returned.

As for your second point, when there is shortage of memory, garbage collection is triggered. Then, the weakly referenced cells are garbage collected and the weak references are invalidated. Therefore, weak references either point to their original location, or to INVALID_LOC. It should not be possible for a weak reference to point to something new as a result of garbage collection. Note also that the weak references themselves should not be garbage collected if they are still within scope.

I hope that helps.

Best,
Iva

yc81reja
Erstie
Erstie
Beiträge: 12
Registriert: 18. Apr 2015 16:25

Re: Weak references problem

Beitrag von yc81reja »

I've mistaken something in concept of weak reference. Thank you.

Antworten

Zurück zu „Archiv“