SCM

[#313553] Valgrind reports problems for simple operations

View Trackers | Bugs | Export CSV

Date:
2012-02-24 13:38
Priority:
3
State:
Closed
Submitted by:
Wayne Booth (clearscene-guest)
Assigned to:
Stéphane Voltz (stefdev-guest)
Category:
backends (drivers)
Group:
None
Resolution:
Fixed
Summary:
Valgrind reports problems for simple operations

Detailed description
I'm writting a test suit for an application that uses SANE. Part of that test suit runs the application on-top of sane, to ensure there are no memory problems. However, I get many many problems around the sane libs.
In an attempt to isolate these I've found that even the simplest operations cause errors.

For example. Using the following simple program:
--------------
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include <sane/sane.h>
#include <sane/saneopts.h>

int main (int argc, char **argv) {

SANE_Status status;
const SANE_Device **SANE_device_list;

status = sane_init(NULL, NULL);
if(status != SANE_STATUS_GOOD) {
perror("sane did not start");
exit(EXIT_FAILURE);
}

status = sane_get_devices (&SANE_device_list, SANE_TRUE);
if(status == SANE_STATUS_GOOD) {
perror("sane_get_devices, was apparently GOOD");
}

sane_exit();

exit(EXIT_SUCCESS);
}
--------------


and building/running with the following:
(In these tests I only have the "test" backend enabled - commented in, in dll.conf)
--------------
rm -f getScannersTest getScannersTest.o
gcc -I. -g -c -o getScannersTest.o getScannersTest.c
g++ -g -o getScannersTest getScannersTest.o -lsane
valgrind --leak-check=full --leak-resolution=high --error-limit=no --tool=memcheck --num-callers=50 --show-below-main=yes --track-origins=yes --track-fds=yes --show-reachable=yes ./getScannersTest
--------------


I get this output:
--------------
$ ./makeGetScannersText
==2198== Memcheck, a memory error detector
==2198== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2198== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==2198== Command: ./getScannersTest
==2198==
==2198== Invalid read of size 4
==2198== at 0x404AFE7: sane_dll_get_devices (string3.h:144)
==2198== by 0x4048552: sane_get_devices (dll-s.c:17)
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198== Address 0x41e7444 is 20 bytes inside a block of size 23 alloc'd
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x404AFCB: sane_dll_get_devices (dll.c:1102)
==2198== by 0x4048552: sane_get_devices (dll-s.c:17)
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198==
sane_get_devices, was apparently GOOD: Success
==2198==
==2198== FILE DESCRIPTORS: 3 open at exit.
==2198== Open file descriptor 2: /dev/pts/0
==2198== <inherited from parent>
==2198==
==2198== Open file descriptor 1: /dev/pts/0
==2198== <inherited from parent>
==2198==
==2198== Open file descriptor 0: /dev/pts/0
==2198== <inherited from parent>
==2198==
==2198==
==2198== HEAP SUMMARY:
==2198== in use at exit: 6,545 bytes in 466 blocks
==2198== total heap usage: 713 allocs, 247 frees, 50,692 bytes allocated
==2198==
==2198== 4 bytes in 1 blocks are definitely lost in loss record 1 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C6010: strdup (strdup.c:43)
==2198== by 0x4032EE8: ???
==2198== by 0x40349DB: ???
==2198== by 0x404A331: init (dll.c:613)
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198==
==2198== 5 bytes in 1 blocks are definitely lost in loss record 2 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x65646F6C: ???
==2198==
==2198== 5 bytes in 1 blocks are definitely lost in loss record 3 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C6010: strdup (strdup.c:43)
==2198== by 0x4032EE8: ???
==2198== by 0x4034969: ???
==2198== by 0x404A331: init (dll.c:613)
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198==
==2198== 5 bytes in 1 blocks are definitely lost in loss record 4 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x785F6C73: ???
==2198==
==2198== 5 bytes in 1 blocks are definitely lost in loss record 5 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x795F6C73: ???
==2198==
==2198== 5 bytes in 1 blocks are definitely lost in loss record 6 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x785F7261: ???
==2198==
==2198== 5 bytes in 1 blocks are definitely lost in loss record 7 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x795F7261: ???
==2198==
==2198== 6 bytes in 1 blocks are definitely lost in loss record 8 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x74706563: ???
==2198==
==2198== 8 bytes in 1 blocks are definitely lost in loss record 9 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C6010: strdup (strdup.c:43)
==2198== by 0x4032EE8: ???
==2198== by 0x4034BC6: ???
==2198== by 0x404A331: init (dll.c:613)
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198==
==2198== 9 bytes in 1 blocks are definitely lost in loss record 10 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x2D6C706F: ???
==2198==
==2198== 10 bytes in 1 blocks are definitely lost in loss record 11 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x656C6572: ???
==2198==
==2198== 12 bytes in 1 blocks are definitely lost in loss record 12 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C6010: strdup (strdup.c:43)
==2198== by 0x4032EE8: ???
==2198== by 0x4034AE2: ???
==2198== by 0x404A331: init (dll.c:613)
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198==
==2198== 13 bytes in 1 blocks are definitely lost in loss record 13 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x646E6167: ???
==2198==
==2198== 13 bytes in 1 blocks are definitely lost in loss record 14 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x74736573: ???
==2198==
==2198== 13 bytes in 1 blocks are definitely lost in loss record 15 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x2D6E6F6D: ???
==2198==
==2198== 14 bytes in 1 blocks are still reachable in loss record 16 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x4048C8A: sanei_config_get_paths (sanei_config.c:126)
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198==
==2198== 14 bytes in 1 blocks are definitely lost in loss record 17 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x403193A: ???
==2198== by 0x403199F: ???
==2198== by 0x4034875: ???
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198==
==2198== 16 bytes in 1 blocks are still reachable in loss record 18 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x401340C: _dl_close_worker (dl-close.c:374)
==2198== by 0x4013BB7: _dl_close (dl-close.c:754)
==2198== by 0x41CCCF1: dlclose_doit (dlclose.c:37)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x41CD339: _dlerror_run (dlerror.c:164)
==2198== by 0x41CCD27: dlclose (dlclose.c:48)
==2198== by 0x404ABDD: sane_dll_exit (dll.c:964)
==2198== by 0x4048743: sane_exit (dll-s.c:84)
==2198== by 0x4069112: __libc_start_main (libc-start.c:226)
==2198== by 0x80484E0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2198==
==2198== 17 bytes in 1 blocks are definitely lost in loss record 19 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x65766E68: ???
==2198==
==2198== 17 bytes in 1 blocks are definitely lost in loss record 20 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x7A7A7565: ???
==2198==
==2198== 18 bytes in 1 blocks are definitely lost in loss record 21 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x626D756D: ???
==2198==
==2198== 20 bytes in 1 blocks are definitely lost in loss record 22 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x62616E64: ???
==2198==
==2198== 28 bytes in 1 blocks are still reachable in loss record 23 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x400CDEE: _dl_map_object_deps (dl-deps.c:470)
==2198== by 0x4012549: dl_open_worker (dl-open.c:263)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x41CCBE8: dlopen_doit (dlopen.c:67)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198==
==2198== 28 bytes in 2 blocks are definitely lost in loss record 24 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x65726873: ???
==2198==
==2198== 36 bytes in 1 blocks are still reachable in loss record 25 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40050AE: local_strdup (dl-load.c:162)
==2198== by 0x40080D6: _dl_map_object (dl-load.c:2155)
==2198== by 0x400CB77: openaux (dl-deps.c:65)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x4012549: dl_open_worker (dl-open.c:263)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x41CCBE8: dlopen_doit (dlopen.c:67)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198==
==2198== 36 bytes in 1 blocks are still reachable in loss record 26 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x400AAFA: _dl_new_object (dl-object.c:161)
==2198== by 0x4006067: _dl_map_object_from_fd (dl-load.c:957)
==2198== by 0x4007EBC: _dl_map_object (dl-load.c:2250)
==2198== by 0x400CB77: openaux (dl-deps.c:65)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x4012549: dl_open_worker (dl-open.c:263)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x41CCBE8: dlopen_doit (dlopen.c:67)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198==
==2198== 41 bytes in 3 blocks are definitely lost in loss record 27 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x6D6F6566: ???
==2198==
==2198== 58 bytes in 4 blocks are definitely lost in loss record 28 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x6F736571: ???
==2198==
==2198== 75 bytes in 5 blocks are definitely lost in loss record 29 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x64616571: ???
==2198==
==2198== 400 bytes in 1 blocks are still reachable in loss record 30 of 32
==2198== at 0x402732C: calloc (vg_replace_malloc.c:467)
==2198== by 0x400FDAC: _dl_check_map_versions (dl-version.c:300)
==2198== by 0x4012898: dl_open_worker (dl-open.c:269)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x41CCBE8: dlopen_doit (dlopen.c:67)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198==
==2198== 632 bytes in 1 blocks are still reachable in loss record 31 of 32
==2198== at 0x402732C: calloc (vg_replace_malloc.c:467)
==2198== by 0x400A8AF: _dl_new_object (dl-object.c:77)
==2198== by 0x4006067: _dl_map_object_from_fd (dl-load.c:957)
==2198== by 0x4007EBC: _dl_map_object (dl-load.c:2250)
==2198== by 0x400CB77: openaux (dl-deps.c:65)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x4012549: dl_open_worker (dl-open.c:263)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198== by 0x41CCBE8: dlopen_doit (dlopen.c:67)
==2198== by 0x400E63E: _dl_catch_error (dl-error.c:178)
==2198==
==2198== 4,977 bytes in 425 blocks are definitely lost in loss record 32 of 32
==2198== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2198== by 0x40C607A: strndup (strndup.c:46)
==2198== by 0x403B0F3: ???
==2198==
==2198== LEAK SUMMARY:
==2198== definitely lost: 5,383 bytes in 459 blocks
==2198== indirectly lost: 0 bytes in 0 blocks
==2198== possibly lost: 0 bytes in 0 blocks
==2198== still reachable: 1,162 bytes in 7 blocks
==2198== suppressed: 0 bytes in 0 blocks
==2198==
==2198== For counts of detected and suppressed errors, rerun with: -v
==2198== ERROR SUMMARY: 27 errors from 26 contexts (suppressed: 19 from 8)
--------------


and if I comment out the sane_get_devices (so we just have sane_init and sane_exit):
--------------
// status = sane_get_devices (&SANE_device_list, SANE_TRUE);
// if(status == SANE_STATUS_GOOD) {
// perror("sane_get_devices, was apparently GOOD");
// }
--------------


We still get errors:
--------------
$ ./makeGetScannersText
==2262== Memcheck, a memory error detector
==2262== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2262== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==2262== Command: ./getScannersTest
==2262==
==2262==
==2262== FILE DESCRIPTORS: 3 open at exit.
==2262== Open file descriptor 2: /dev/pts/0
==2262== <inherited from parent>
==2262==
==2262== Open file descriptor 1: /dev/pts/0
==2262== <inherited from parent>
==2262==
==2262== Open file descriptor 0: /dev/pts/0
==2262== <inherited from parent>
==2262==
==2262==
==2262== HEAP SUMMARY:
==2262== in use at exit: 14 bytes in 1 blocks
==2262== total heap usage: 112 allocs, 111 frees, 35,839 bytes allocated
==2262==
==2262== 14 bytes in 1 blocks are still reachable in loss record 1 of 1
==2262== at 0x4028876: malloc (vg_replace_malloc.c:236)
==2262== by 0x4048C8A: sanei_config_get_paths (sanei_config.c:126)
==2262== by 0x4069112: __libc_start_main (libc-start.c:226)
==2262== by 0x80484A0: ??? (in /home/wbooth/git/clearscene-src-opendias/contrib/getScannersTest)
==2262==
==2262== LEAK SUMMARY:
==2262== definitely lost: 0 bytes in 0 blocks
==2262== indirectly lost: 0 bytes in 0 blocks
==2262== possibly lost: 0 bytes in 0 blocks
==2262== still reachable: 14 bytes in 1 blocks
==2262== suppressed: 0 bytes in 0 blocks
==2262==
==2262== For counts of detected and suppressed errors, rerun with: -v
==2262== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 6)
--------------

I would love to be able to eliminate these errors, so that I can relably use valgrind to test my application.

Cheers.

Followups: Sort comments antichronologically

Message
Date: 2015-08-07 10:09
Sender: Stéphane Voltz

Your programm has to free the resources allocated by sane_get_devices()

i.e walk the **SANE_device_list list and free the pointers.
Date: 2015-08-08 07:20
Sender: Stéphane Voltz

Sorry in fact this is a memleak in the test backend. The test program is fine.
Date: 2015-08-08 19:47
Sender: Stéphane Voltz

The memleaks in the test backend have been fixed by commit 1f1385ea671d2cc1df2238cf580438cf2255bdfb

Attached Files:

Changes:

Field Old Value Date By
status_idPending2015-08-10 20:21stefdev-guest
close_date2015-08-08 19:472015-08-10 20:21stefdev-guest
ResolutionAccepted2015-08-08 19:47stefdev-guest
status_idOpen2015-08-08 19:47stefdev-guest
close_dateNone2015-08-08 19:47stefdev-guest
ResolutionRejected2015-08-08 07:20stefdev-guest
status_idPending2015-08-08 07:20stefdev-guest
close_date2015-08-07 10:092015-08-08 07:20stefdev-guest
assigned_tonone2015-08-08 07:20stefdev-guest
ResolutionNone2015-08-07 10:09stefdev-guest
status_idOpen2015-08-07 10:09stefdev-guest
close_dateNone2015-08-07 10:09stefdev-guest
Powered By FusionForge