False errors from valgrind

I’ve run into a slight problem when running a glib application in valgrind (using G_SLICE=debug-blocks and MALLOC_CHECK_=2 but I’m not sure whether they make a difference). I get a lot of false “Invalid read” error reports from valgrind. The reads are all within bounds of blocks that haven’t been freed, so I guess it thinks the memory being read hasn’t been initialised. The errors all seem to be when trying to read from an object’s instance struct. They come from GObject’s own code too, not just mine.

The GObject construction process explicitly initialises such structs (and objects’ private data structs, I think) to 0, so these reads shouldn’t be a problem. So why is valgrind not picking up on that?

Add --suppressions=/usr/share/glib-2.0/valgrind/glib.supp to your valgrind invocation.

You probably also want to use G_SLICE=always-malloc (or leave the G_SLICE environment variable unset), as GSlice doens’t interact well with valgrind. If you leave the environment variable unset it will default to G_SLICE=always-malloc if (and only if) your application is running under valgrind.

1 Like

Thanks, that’s fixed it for me.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.