I have a question about memory management when loading shared library. In C/C++, after finishing using a library, one needs to unload it to ensure that:
- memory is properly deallocated, and
- the system could do some clean-up, like closing file handlers, ...
We have this C program:
Code: Select all
/* compile with:
* $ gcc <filename> -fPIC -shared -o <libname>
* or
* $ tcc <filename> -shared -o <libname>
**/
#include <string.h>
#include <stdlib.h>
typedef struct mystruc
{
int number;
char *ptr;
} MyStruc;
MyStruc *foo3(char *ptr, int num)
{
MyStruc *astruc;
astruc = malloc( sizeof( MyStruc ) );
astruc->ptr = malloc( len( ptr ) + 1 );
strcpy( astruc->ptr, ptr );
astruc->number = num;
return astruc;
}
Code: Select all
(import "mystruc.so" "foo3")
(setq 'astruc (foo3 "hello world" 123))
(get-string (get-integer (+ a struc 4))) ;; 4 = sizeof int in C
(get-integer astruc)