On Mon, Jun 20, 2016 at 1:08 PM, Ian Lance Taylor <i...@golang.org> wrote:

> On Mon, Jun 20, 2016 at 10:39 AM, Jason E. Aten <j.e.a...@gmail.com>
> wrote:
> > This does raise an interesting question: how should one detect cycles in
> a
> > graph of Go objects, in a garbage-collection safe manner?
> >
> > I am using reflect.ValueOf(x).Pointer() at the moment (which gives an
> > uintptr), but this seem vulnerable to having the garbage collector move
> the
> > object in the middle of my graph traversal.
>
> Instead of converting the pointer to a uintptr, just store the pointer
> itself in a map[interface{}]bool.  If you encounter the same pointer
> again, you will get the same map entry.  The GC must guarantee that
> using pointers as map keys will work even if the pointers move.
>
> Ian
>

Brilliant! Thank you, Ian!

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to