This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[Q] automagic define/undefine with dynamic-link/dynamic-unlink


	Hello,

I want to use the _init and _fini symbols to automatically
define/undefine extensions as a dynamic library is linked/unlinked. My
naive approach looks like:

mysin.c:
#include <math.h>
#include <guile/gh.h>
SCM mysin(SCM x) {return gh_double2scm(sin(gh_scm2double(x)));}
void _init(void) {gh_new_procedure("mysin", mysin, 1, 0, 0);}
void _fini(void) {gh_eval_str("(undefine mysin)");}

Under Linux, this compiles with
$ gcc -c -fPIC mysin.
$ ld -shared -o mysin.so mysin.o

I test mysin.so with:
$ guile
guile> (define obj (dynamic-link "./mysin.so"))
guile> (mysin 1)
0.841470984807897
guile> (dynamic-unlink obj)
guile> (mysin 1)
ERROR: In expression (mysin 1):
ERROR: Unbound variable: mysin
ABORT: (misc-error)

Type "(backtrace)" to get more information.

... which is exactly what I would expect ...

Question: guile seems to leak memory each time the test is repeated. I
am looking for some `gh_delete_procedure' that guarantees that all
resources allocated by gh_new_procedure are released.

Second question: If I remove _init and _fini, "./mysin.so" is not able
to do anything useful. Still (dynamic-unlink (dynamic-link "./mysin.so")) 
seems to consume memory. Could this be a problem with the
dlopen/dlclose implementation?

Third question: Could something like guile-snarf be used to scan a c
source file for extensions and put the appropriate
gh_new_procedure/gh_delete_procedure calls in _init/_fini?

	Regards,

	Ole Myren Røhne