device.c: Make sr_*_inst_free() tolerant to NULL parameters

g_free() is already tolerant to NULL pointers. If any sigrok internal free
function is passed a partially initialized structure (i.e. some fields are
null), it will safely free it. However, if the struct pointer itself is NULL, it
will dereference it, creating a segmentation fault.

Check for NULL pointers in all *_free() functions in device.c. This makes them
tolerant to NULL pointers, and more closely mimics the behavior of g_free().

Unavoidably, this patch also contains a memory leak fix for sr_dev_inst_free().
The sr_probe structures in sdi->probes were freed, but the GSList structure
itself was not freed ("definitely lost" memory leak).

Tested with Radioshack 22-812 connected to /dev/ttyUSB10:
$ G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --leak-check=full \
sigrok-cli --driver=radioshack-22-812:conn=/dev/ttyUSB10 --continuous -O analog

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
serial-dmm: Rename scan() to serial_dmm_scan()

There is no technical reason to do this. serial_dmm_scan() is static, so there
will be no name conflicts. The real value in this change comes when running
valgrind. If all scan functions are named scan(), then it becomes difficult to
tell from valgrind output which scan() caused the memory leak.

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
serial-dmm: Simplify clear_instances()

Use g_slist_free_full() instead of traversing the list and freeing each element
manually.

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
serial-dmm: Add separate error exit path for serial_dmm_scan()

The problem with having a single exit path for serial_dmm_scan was that we would
not know if we encountered an error or not. When we encounter error, we need to
clear any allocated resources. This was not done, causing memory leaks when
hw_scan() was invoked, but no device was connected.

Having a separate exit path for errors allows us to properly clean up any
allocated resources. We can simply free all our resources in the error path,
since our cleanup helpers are NULL-tolerant.

For example, when the device was not detected, struct sr_serial_dev_inst *serial
was never freed, causing a "definitely lost" memory leak.

Other possible failure points were simulated by replacing the if(!malloc())
clauses with if(!0).

Tested with no device attached
$ G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --leak-check=full \
sigrok-cli --driver=radioshack-22-812:conn=/dev/ttyUSB10 --continuous -O analog

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
session: Remove datafeed callbacks when destroying session

The datafeed callback list was not freed when a session was destroyed. This
caused a "definitely lost" memory leak.

Remove all datafeed callbacks when destroying the session. This causes the
session->datafeed_callbacks list to be freed, and prevents the memory leak.

Tested with Radioshack 22-812 connected to /dev/ttyUSB10:
$ G_SLICE=always-malloc G_DEBUG=gc-friendly valgrind --leak-check=full \
sigrok-cli --driver=radioshack-22-812:conn=/dev/ttyUSB10 --continuous -O analog

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>