On Jan 9, 2012, at 11:00 50PM, Nicolas Cellier wrote: > 2012/1/9 Eliot Miranda <[email protected]>: >> >> >> On Mon, Jan 9, 2012 at 1:16 PM, Mariano Martinez Peck >> <[email protected]> wrote: >>> >>> Ok guys....what about the following: >>> >>> stronglyPointsTo: anObject >>> >>> "Answers true if the garbage collector would fail to collect anObject >>> because I hold a reference to it, or false otherwise" >>> >>> (self instVarsInclude: anObject) >>> ifTrue: [ >>> self class isWeak ifFalse: [ ^true ]. >>> 1 to: self class instSize do: [ :i | >>> (self instVarAt: i) == anObject ifTrue: [ ^true ] ]. >>> ^false ] >>> ifFalse: [ ^self class == anObject and: [ self class isCompact not >>> ] ] >> >> >> Named inst vars in weak arrays are strong references. So the above looks >> wrong to me. Its more like this: >> >> 1 to: self class instSize do: >> [:i| (self instVarAt: i) == anObject ifTrue: [ ^true ] ]. >> self class isWeak ifFalse: >> [1 to: self basicSize do: >> [:i| (se;f basicAt: i) == anObject ifTrue: [ ^true ] ]. >> ^false >> > > > But instVarIncludes: does walk over all the indexed slots, not just > the inst vars (yeah, the name does not tell) > > 1 to: self class instSize do: > [:i| (self instVarAt: i) == anObject ifTrue: [ ^true ] ]. > 1 to: self basicSize do: > [:i| (self basicAt: i) == anObject ifTrue: [ ^true ] ]. > ^false > > So I think this part of the method works... > > Only the class test looks strange, (WeakArray new) does point strongly > to its class like Henrik mentioned with his (WeakArray with: > WeakArray) joke - or I didn't understand ;)
Yes, that was my point, sorry for the murkiness with which I tried to make it. In Levente's code: anObject instVarsInclude: itsClass -> true class is weak -> true ref in weak slot -> true And all of a sudden, you don't pointTo: your class anymore just because you have it in a weak slot. (The test would fail) Cheers, Henry
