Hoodoo
Hoodoo

Reputation: 79

gotk3 foreach loop, losing a pointer and gaining memory

I have a packet forwarding application that is mostly working quite well. There is a gtk3 foreach loop that runs every 2 seconds to update channel forwarding statistics in the treeview. To save resources this only runs when the treeview window has the focus.

MainListStore.ForEach(func(tmodel *gtk.TreeModel, path *gtk.TreePath, iterfe *gtk.TreeIter) bool {
// get the channel no
value, _ := tmodel.GetValue(iterfe, MCOL_INPORT)
goValue, _ := value.GoValue()
key := goValue.(int)
// copy stats to liststore
Mutex.Lock()
err := MainListStore.Set(iterfe,
[]int{MCOL_STATPIX, MCOL_STATINT, MCOL_SPDIN, MCOL_SPDOUT},
[]interface{}{Pixes[Statmap[key][0]], Statmap[key][0], Statmap[key][1], Statmap[key][2]})
Mutex.Unlock()
if err != nil {
Logit.Printf("Error: error updating stats chan %s, %v", key, err)
}
return false // keep iterating
})

1: bad pointer crash. I kept this process running for some time and eventually got a crash:

runtime: bad pointer in frame github.com/gotk3/gotk3/gtk.(*TreeModel).ForEach.func1 at 0xc0007df378: 0x2

fatal error: invalid pointer found on stack

My best guess is that between starting the loop and setting values in the liststore, the Treeiter became invalid.

Any suggestions as to what went wrong and how to avoid it would be appreciated.

Is there a way to streamline getting the int key from treemodel? Rather than declaring a *gtk.value, then a govalue interface then the int? Most of which presumably end up on the heap, which brings me to:

  1. Memory use. When this loop is not running, the program runs with about 25MB of RAM. When the loop does run, memory in use very slowly increases, which is probably unavoidable, the interesting thing is that when the window is returned to the background and the loop no longer runs, go does not release the extra memory, it continues to run with whatever the new level is. It would be nice if it could slowly release the memory again.

Thanks for any expert advice!

Upvotes: 0

Views: 49

Answers (0)

Related Questions