-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Closed
Labels
Milestone
Description
I am having a garbage collection crash (dump) in 1.4 in the code snipped below.
In 1.3.3, the same program prints "Hello" and successfully exits:
package main
import (
"log"
"reflect"
"sync"
)
type Bus struct {
listeners map[reflect.Type][]reflect.Value
}
// Register adds a new event listener.
// Assumes a function with one parameter as fn argument
func (b *Bus) Register(fn interface{}) {
v := reflect.ValueOf(fn)
t := v.Type()
// Map the argument type to the listener.
arg := t.In(0)
b.listeners[arg] = append(b.listeners[arg], v)
}
// Emit dispatches an event to all registered listeners.
// A WaitGroup is returned which can optionally be used to block on all handlers
// completing (note: the bus makes no attempt to ensure the handlers actually
// complete).
func (b *Bus) Emit(event interface{}) *sync.WaitGroup {
var v reflect.Value
args := make([]reflect.Value, 1)
v = reflect.ValueOf(event)
args[0] = v
var wg sync.WaitGroup
for _, fn := range b.listeners[v.Type()] {
wg.Add(1)
go func() {
fn.Call(args)
wg.Done()
}()
}
return &wg
}
func main() {
bus := &Bus{
listeners: map[reflect.Type][]reflect.Value{},
}
bus.Register(func(i int) {
log.Println("Hello")
})
wg := bus.Emit(1)
wg.Wait()
}
My go version is 1.4 in OSX, using Homebrew package management:
$ go version
go version go1.4 darwin/amd64
For reference: golang-nuts thread on the issue