prevent garbage collection on an object in external code - Maple Help

Online Help

All Products    Maple    MapleSim

Home : Support : Online Help : Programming : OpenMaple : C Application Programming Interface : Memory and Foreign Object Management : OpenMaple/C/MapleGc

MapleGcProtect - prevent garbage collection on an object in external code

MapleGcAllow - allow garbage collection on an object in external code

MapleGcIsProtected - test if an object is protected from garbage collection in external code

MapleGcMark - mark an object contained in a MaplePointer during a garbage collection

Calling Sequence

MapleGcProtect(kv, s)

MapleGcAllow(kv, s)

MapleGcIsProtected(kv, s)

MapleGcMark(kv, s)




kernel handle of type MKernelVector



type ALGEB object



These functions can be used in external code with OpenMaple or define_external.


MapleGcProtect prevents the object, s, from being collected by the Maple garbage collector. The memory pointed to by s is not freed until Maple exits, is restarted, or a call to MapleGcAllow is issued. Any Maple objects that must persist between external function invocations must be protected, or associated with a MaplePointer mark function. This includes any external global or static ALGEB variables that will be referred to in a later external call. Failure to protect such a persistent variable can lead to unexpected results if the Maple garbage collector disposes of it between function calls.


MapleGcAllow allows the Maple garbage collector to reclaim storage used by the object, s.  This does not necessarily mean that the storage will be reclaimed.  It just means that the object obeys the same rules applied to all other Maple objects -- that they can be collected whenever there is no longer anything actively referring to them.  Be careful not to allow garbage collection on any object that was not protected by you, or was protected prior to when your code was to protect it.


MapleGcIsProtected returns TRUE if the given object is protected from garbage collection.


MapleGcMark is used in combination with the MaplePointer mark function callback supplied by MaplePointerSetMarkFunction. It must be called only by such a callback. It is recommended that you use a MaplePointer with a mark function instead of protecting and unprotecting objects.


    #include "maplec.h"

    ALGEB M_DECL MySaveLast( MKernelVector kv, ALGEB *args )


    static ALGEB last = NULL;

    static M_BOOL was_protected = FALSE;

        ALGEB prev;

    if( !last ) {

        last = ToMapleNULL(kv);

        was_protected = MapleGcIsProtected(kv,last);



    if( MapleNumArgs(kv,(ALGEB)args) > 0 ) {

        if( !was_protected )


            prev = last;

        last = args[1];

        was_protected = MapleGcIsProtected(kv,last);


            return( prev );


    return( last );


Execute the external function from Maple.











See Also

CustomWrapper, define_external, gc, OpenMaple, OpenMaple/C/API, OpenMaple/C/Examples, OpenMaple/C/MaplePointer

Download Help Document

Was this information helpful?

Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam