package main

import (
   "sync"
   "runtime"
)

type S struct {
   chs chan int
}

var wg sync.WaitGroup

func worker(s *S) {
   for i := range s.chs {
      println("In worker, ch = ", i)
   }

   wg.Done()
}

func main() {
   s := S{make(chan int)}

   runtime.SetFinalizer(&s, func(ss *S) {
      println("Finalizer")
      close(ss.chs)
   })


   wg.Add(1)

   go worker(&s)
   for i := 0; i < 1; i++ {
      s.chs <- 1
   }

   runtime.GC()

   wg.Wait()
}


*Output (Go 1.8.3):*


In worker, ch =  1
Finalizer



---

As my expectation,  runtime.GC() will not collect `s` , because `worker` 
still holds a reference to `s.chs`. So that the program would deadlock.

However, this program terminates in go 1.8.3, while deadlocks in go 1.7.

Does something change in go 1.8.3? 

I guess it has something to do with `range` and `channel`, but know nothing 
more .

Does anyone know what happens  to this garbage collection process?

Thanks very much for reading this.


-- 
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