MapleGcProtect - Maple Help

Online Help

All Products    Maple    MapleSim

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


prevent garbage collection on an object in external code


allow garbage collection on an object in external code


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


Calling Sequence




Calling Sequence

MapleGcProtect(kv, s)

MapleGcAllow(kv, s)

MapleGcIsProtected(kv, s)




kernel handle returned by StartMaple



Maple object



These functions are part of the OpenMaple interface to Microsoft Visual Basic.


The MapleGcProtect function 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. This includes any external global or static Maple objects that will be referred to at a later time. Failure to protect such a persistent variable can lead to unexpected results if the Maple garbage collector disposes of it between function calls.


The MapleGcAllow function 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 means that the object obeys the same rules applied to all other Maple objects, that is, objects can be collected when they are no longer actively referred to. Ensure that you only unprotect objects that were protected by your call to MapleGcProtect. Do no use MapleGcAllow on objects that you did not protect.


Another way to prevent an object from being garbage collected is to assign it as the value of any global name.


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


Sub KeepResults(ByVal kv As Long)

    Dim table, r, i As Long

    table = MapleTableAlloc(kv)

    'important to protect this table so it isn't collected

    MapleGcProtect kv, table

    For i = 1 To 10

        r = EvalMapleStatement(kv, "rand();")

        MapleTableAssign kv, table, ToMapleInteger(kv, i), r

    Next i

    MapleGcAllow kv, table

End Sub

See Also






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