I am trying to store some complicated data structure with a map, and eventually search and use it. Since the structure can change with different applications, and it has to work with multiple thread, I defined a generic map like follows
type IndexedMap struct { sync.RWMutex DataNode map[interface{}]interface{} } Insert is defined as func (m *IndexedMap) Insert(dataIndex interface{}, data interface{}) error { var err error m.Lock() //if the node exist if _, ok := m.DataNode[dataIndex]; ok { m.Unlock() return m.Update(dataIndex, data) } else { //insert new node m.DataNode[dataIndex] = data } m.Unlock() return err } ** For now, the data being inserted is a structure, not the pointer to the structure and the key/index is a string.** Then when I need to find a piece of data, I tried to get a snapshot of the database first and then look for it v, ok : = m.SnapShotData()[key] where func (m *IndexedMap) SnapShotData() map[interface{}]interface{} { ret := make(map[interface{}]interface{}) m.Lock() for k, v := range m.DataNode { ret[k] = v } m.Unlock() return ret } Then the data v is used to do other things, never modified though. This works but the performance is abysmal when the database getting large. So, I tried to search without copying v, ok := m.DataSearch(key) func (m *IndexedMap) DataSearch(dataIndex interface{}) (interface{}, bool) { var data interface{} var ok bool m.Lock() data, ok = m.DataNode[dataIndex] m.Unlock() return data, ok } However, with this implementation, I always get crash after running the program just a little bit with the following error *** Error in `./colordecoder': double free or corruption (fasttop): 0x00007f943c26f590 *** SIGABRT: abort PC=0x7f946c2eac37 m=4 signal arrived during cgo execution I guess somewhere along the line the copy is a not a hard copy but just a reference copy. However, I couldn't understand the difference between above two approaches, seems that in SnapShotData I did exactly the same thing, but no crash ever. Can somebody tell me where I did wrong? Thanks!! -- 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.