how to work with weak referenses

  • Thread starter Thread starter Boni
  • Start date Start date
B

Boni

Dear all,
------
Dim _w as weakreference
func XXX as mytype
(1)if _w.isalive orelase _w.target is nothing then return nothing
(2)return _w.target
end func
 
Boni said:
Dear all,
------
Dim _w as weakreference
func XXX as mytype
(1)if _w.isalive orelase _w.target is nothing then return nothing
(2)return _w.target
end func
----
How is it guaranted, that between (1) and(2)
GC will not remove _w?
Thanks,
Boni

You have declared _w in a class here I assume, since it is not in a
function. That being the cass _w will not be released until after the
class goes out of scope. This means as long as the class is alive, _w
will be. GC can do nothing to _w withing your function you show here

Chris
 
Oooh, I am sorry, my question is incorrect.
It should be:
How is it guaranted, that between (1) and(2) GC will not collect the object
on which _w points.
 
Boni said:
Oooh, I am sorry, my question is incorrect.
It should be:
How is it guaranted, that between (1) and(2) GC will not collect the object
on which _w points.

I still think my answer applies. As long as there is a reference to the
object, then GC will not collect it. _w is declared outside of the
function so there is no way that the object that _w points to will be
collected until something like _w = nothing happens.

Chris
 
Boni,
There is no guarantee that I know of.

However if the GC did remove the target of _w, then _w.Target will return
Nothing...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Dear all,
| ------
| Dim _w as weakreference
| func XXX as mytype
| (1)if _w.isalive orelase _w.target is nothing then return nothing
| (2)return _w.target
| end func
| ----
| How is it guaranted, that between (1) and(2)
| GC will not remove _w?
| Thanks,
| Boni
|
|
|
 
Chris,
| I still think my answer applies. As long as there is a reference to the
| object, then GC will not collect it. _w is declared outside of the
| function so there is no way that the object that _w points to will be
| collected until something like _w = nothing happens.
No, Boni is asking about WeakReference.Target, not the WeakReference itself.

_w is the WeakReference object itself & is subject to normal GC rules (what
you have been stating)

_w.Target is the "weak reference", and is subject to special GC rules, if
the only reference to the object that _w.Target refers to is _w.Target
itself & the GC needs more memory, then it can release the object that
_w.Target refers to. In which case _w.IsAlive will be false & _w.Target will
return false.

In other words _w.IsAlive is short hand for (_w.Target IsNot Nothing)


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Boni wrote:
| > Oooh, I am sorry, my question is incorrect.
| > It should be:
| > How is it guaranted, that between (1) and(2) GC will not collect the
object
| > on which _w points.
| >
| >
| > | >
| >>Boni wrote:
| >>
| >>>Dear all,
| >>>------
| >>>Dim _w as weakreference
| >>>func XXX as mytype
| >>>(1)if _w.isalive orelase _w.target is nothing then return nothing
| >>>(2)return _w.target
| >>>end func
| >>>----
| >>>How is it guaranted, that between (1) and(2)
| >>>GC will not remove _w?
| >>>Thanks,
| >>>Boni
| >>>
| >>>
| >>>
| >>
| >>You have declared _w in a class here I assume, since it is not in a
| >>function. That being the cass _w will not be released until after the
| >>class goes out of scope. This means as long as the class is alive, _w
| >>will be. GC can do nothing to _w withing your function you show here
| >>
| >>Chris
| >
| >
| >
|
| I still think my answer applies. As long as there is a reference to the
| object, then GC will not collect it. _w is declared outside of the
| function so there is no way that the object that _w points to will be
| collected until something like _w = nothing happens.
|
| Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top