C
Chris Mullins
If I have an object held only by a weak reference, and I write code that
looks like:
dim strongRef as Object
if wr.IsAlive() then
strongRef=wr.Target
else
strongRef=new MyObject()
end if
There is a fairly signifigant race condition - between the "wr.IsAlive" call
and the wr.Target call, the object could be garbage collected. To the best
that I can find, there is no mechanism by which I can lock the Weak
Reference to prevent a GC between my IsAlive check, and the rooting of the
object.
To make this code legit, and work in all cases it needs to look like:
dim strongRef as Object
if wr.IsAlive() then
strongRef=wr.Target
if strongRef is nothing then strongRef = new MyObject()
else
strongRef=new MyObject()
end if
It seems like there should be a way to avoid that extra check for the
"IsAlive" case.
What am I missing?
looks like:
dim strongRef as Object
if wr.IsAlive() then
strongRef=wr.Target
else
strongRef=new MyObject()
end if
There is a fairly signifigant race condition - between the "wr.IsAlive" call
and the wr.Target call, the object could be garbage collected. To the best
that I can find, there is no mechanism by which I can lock the Weak
Reference to prevent a GC between my IsAlive check, and the rooting of the
object.
To make this code legit, and work in all cases it needs to look like:
dim strongRef as Object
if wr.IsAlive() then
strongRef=wr.Target
if strongRef is nothing then strongRef = new MyObject()
else
strongRef=new MyObject()
end if
It seems like there should be a way to avoid that extra check for the
"IsAlive" case.
What am I missing?