[sane-devel] SANE V2

Diego Zuccato diego@otello.alma.unibo.it
Mon, 09 Dec 2002 00:13:01 +0000

Henning Meier-Geinitz wrote:

> And this would be returned by any SANE API function that currently
> returns SANE_Status? Nice, because it links the error number and the
> message. No need for an extra call to a function.
Yep. That's why I like this method. :-)

> However: How long is errmsg valid? Until the next call to any SANE
> function? That's bad because the frontend may get in trouble when
> using threads. Until sane_exit()? Then the memory usage gets bigger
> and bigger.
Well, if it points to a static string (it's const char*) the only limit
for the string is available memory :-)
The struct should be allocated (from heap or from a pool) by the
function called and then freed by the caller. And that's what I don't
like in strdup() like function, but sometimes it's necessary. Another
solution, rarely used, consists of passing a ptr to the struct, so the
called function can set it up as needed and the "return value" is just a
flag ("all right" or "look at the struct").

> The really interesting error messages are the dynamic ones: "Couldn't
> open %s". And %s can be anything and can change multiple times between
> sane_init and sane_exit.
Err... I didn't think about these. :-(
Well, in that case, use the strdup() technique: the called function
allocates the needed memory that must be freed by the caller. I heard
(well, seen in some code) that that could be a problem under winkozz,
since if a dll allocates memory, that memory must be freed from the dll
itself. Just add a free() wrapper in the dll code... May be SANE_free()

> Yet another way would be to use an additional callback:
Callbacks always give me nightmares (I'm developing a db async access
infrastructure that interacts with gtk... brr!)

> If only C would copy strings when returning them as the return value
> of functions :-)
Simple :
return strdup(errmsg);
instead of :
return errmsg;

PS: for multithreaded programs, it's possible to setup a thread-specific
data area... See pthread.h .