Memory management in shared library

Started by cmpitg, May 21, 2011, 08:36:53 PM

Previous topic - Next topic

cmpitg

Hi newLISPers,



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, ...
  • [/list]


    In newLISP, should/must we use the same method? If yes, then how to do it properly.  Let's take the example in newLISP Code Patterns:



    We have this C program:



    /* 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;
    }


    After compiling it to a shared library, we can import ``MyStruc`` to our newLISP program by using:



    (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)


    Do we have to manually deallocate memory allocated to ``astruc``?  According to what I understand, since ``astruc`` is created by newLISP, exists in newLISP environment, newLISP's garbage collector automatically takes care of memory management for us.  Right?
    \"Life is a hack\"

    My web log: http://cmpitg.wordpress.com\">//http://cmpitg.wordpress.com

    Lutz

    #1
    'astruc' will contain a pointer to the memory allocated during the C call to foo3(). And you have to de-allocate that memory manually - it is not managed by newLISP. You could simply import free() from libc and then free it:


    (import "/usr/lib/libc.so" "free")

    (free astruc)


    When you do:



    (set 'myvar (get-string (get-integer (+ astruc 4))))


    then 'myvar' would contain a copy of "hello world" and that memory in 'myvar' would be managed by newLISP.



    Often when calling functions part of the MS Window API, you have to pre-allocate memory in newLISP, then pass a pointer to that function to place some stuff into that memory area:


    (import "kernel32.dll" "GetWindowsDirectoryA")
    (set 'str (dup "00" 64)  ; reserve space and initialize

    (GetWindowsDirectoryA str (length str))

    (trim str)  → "C:\WINDOWS"


    this example is from the manual entry for 'import'. In this case too, newLISP will take care of memory management. When the variable 'str' gets reused for something else, then the old contents gets freed automatically by newLISP.

    cmpitg

    #2
    Thank you, Lutz.  That makes sense.  Let me summarize a little bit to see if I really understand it right.  Please correct me if I'm wrong:



    [*] Any allocation performed by foreign functions (functions which are imported from shared libraries) has to be deallocated manually if there's no call to do so.
  • [*] In case of calling foreign functions with passing by reference, memory for variables need to be allocated beforehand by newLISP, and hence, they need not be deallocated manually.
  • [/list]


    Examples:


    [list=1]
  • [*] Taken from newLISP Code Patterns, one needs to free the memory with C's ``free`` function.

  • [*] Using newLISP with gtk-server. No manual deallocation should be taken since gtk-server does that after the call for ``gtk_quit()`` or so.

  • [*] Taken from the reference of ``import`` function in newLISP Manual, which you have just mentioned.
  • [/list]
    \"Life is a hack\"

    My web log: http://cmpitg.wordpress.com\">//http://cmpitg.wordpress.com

    cmpitg

    #3
    Should these notes be available on newLISP Code Patterns or newLISP Manual and Reference, guys?  I think extra cautions, especially cautions about memory, are never redundant.  Right?
    \"Life is a hack\"

    My web log: http://cmpitg.wordpress.com\">//http://cmpitg.wordpress.com