From 7e318fb1646da6726dd60c5ceebb987f70826310 Mon Sep 17 00:00:00 2001 From: Petter Reinholdtsen Date: Mon, 12 Feb 2001 19:31:01 +0000 Subject: [PATCH] Get rid of small memory leak when using 'scanimage -L'. --- backend/dll.c | 21 +++++++++++++++++++-- backend/v4l.c | 17 +++++++++++------ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/backend/dll.c b/backend/dll.c index 6acb459ec..284a08dd8 100644 --- a/backend/dll.c +++ b/backend/dll.c @@ -178,6 +178,13 @@ struct alias char *newname; }; +/* + * List of available devices, allocated by sane_get_devices, released + * by sane_exit() + */ +static const SANE_Device **devlist = NULL; +static int devlist_size = 0, devlist_len = 0; + static struct alias *first_alias; static SANE_Auth_Callback auth_callback; static struct backend *first_backend; @@ -584,6 +591,18 @@ sane_exit (void) free(alias->oldname); free(alias); } + + if (NULL != devlist) + { /* Release memory allocated by sane_get_devices(). */ + int i = 0; + while (devlist[i]) + free(devlist[i++]); + free(devlist); + + devlist = NULL; + devlist_size = 0; + devlist_len = 0; + } } /* Note that a call to get_devices() implies that we'll have to load @@ -594,8 +613,6 @@ sane_exit (void) SANE_Status sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only) { - static int devlist_size = 0, devlist_len = 0; - static const SANE_Device **devlist; const SANE_Device **be_list; struct backend *be; SANE_Status status; diff --git a/backend/v4l.c b/backend/v4l.c index a7c8a917c..5b2cc6a7c 100644 --- a/backend/v4l.c +++ b/backend/v4l.c @@ -108,6 +108,7 @@ #include "sane/sanei_config.h" #define V4L_CONFIG_FILE "v4l.conf" +static const SANE_Device ** devlist = NULL; static int num_devices; static V4L_Device * first_dev; static V4L_Scanner * first_handle; @@ -179,6 +180,9 @@ attach (const char *devname, V4L_Device **devp) { V4L_Device * q; + if (!devp) + return SANE_STATUS_INVAL; + errno = 0; q = malloc (sizeof (*q)); @@ -199,12 +203,8 @@ attach (const char *devname, V4L_Device **devp) first_dev = q; - if (devp) - *devp = q; + *devp = q; return SANE_STATUS_GOOD; - - free (q); - return SANE_STATUS_INVAL; } @@ -626,12 +626,17 @@ sane_exit (void) free ((void *) dev->sane.name); free (dev); } + + if (NULL != devlist) + { + free(devlist); + devlist = NULL; + } } SANE_Status sane_get_devices (const SANE_Device *** device_list, SANE_Bool local_only) { - static const SANE_Device ** devlist = 0; V4L_Device *dev; int i;