Pointers or Handles or...?

BlitzMax Forums/BlitzMax Programming/Pointers or Handles or...?

Rozek(Posted 2006) [#1]
Hello!

I have a (typical beginner's?) question:

I plan to create objects of various kinds within BlitzMax functions, which get called from a Lua script (the Lua environment itself has been setup by BlitzMax and the script has also been called from within BlitzMax). These objects are then passed back to Lua.

Of course, I want the BlitzMax objects to "survive", i.e. not to be garbage collected until they have been (explicitly) released by a BitzMax function (which gets called by Lua, when Lua's GC decides to release the corresponding Lua object)

My question: what is the best way to achieve this behaviour? Should I

- pass VarPtrs to and from Lua? (how gets the Ptr's target object kept in memory then?)
- create "Handles" and pass them to/from Lua? (what type of data are "handles"? do they "refer" to the target in a GC's sense? how do I "release" a handle then?)
- use another approach I do not yet know about?

Thanks in advance for any response!


Mordax_Praetorian(Posted 2006) [#2]
I have no idea how to use Lua, however

As long as there is something (A list, a variable, a TMap, anything) that refers to the object, the garbage collector wont touch it


skidracer(Posted 2006) [#3]
Yes, simplest way would be to keep them in a list and have any creator functions you call from lua add the object to the list and have a ReleaseObject function that removes them:
Strict

Global luaobjects:TList=New TList

Function MakePixmap:TPixmap(width,height,format)
	Local pix:TPixmap
	pix=CreatePixmap(width,height,format)	
	luaobjects.AddFirst pix
	Return pix
End Function

Function ReleaseObject(o:Object)
	luaobjects.Remove o
End Function


undocumented and I'll probably get growled at for suggesting it but you could probably skip the list method and declare and use the following two internal functions directly:

Extern "C"
Function bbObjectRetain(o:Object)
Function bbObjectRelease(o:Object)
End Extern



Rozek(Posted 2006) [#4]
Good morning!

Given, that one has to "release" an object handle explicitly (according to the docs), would it be sufficient to make a handle from a BlitzMax object, pass it to Lua as "light userdata" and - when Lua decides to destroy that object - "release" the handle on the BlitzMax side?

(I'll have to check whether "light" userdata would be sufficient with regard to GC, it might be better to use "ordinary" userdata)