lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


diego@tecgraf.puc-rio.br wrote:
>
> Hi,
> 
> > If the current scheme is to be modified:
> > 1) Unloadlib support please. (For Win32 - FreeLibrary())
> 
> This will require some support in C. Probably the best way
> to do this would be to have a GC method associated with the
> namespace table.

IMHO, that would be too fragile - a script could crash the application[1].

What about the method outlined below?

|[ http://lua-users.org/lists/lua-l/2002-03/msg00199.html ]
|
| I meant real garbage collection.  If there's no reference to the dynamic
| binary it should be unloaded.  Because the only thing it can export are
| functions that's not that difficult.
|
| At the start you only have the startfunc.  All future pushcclosure with
| a function from that binary has to be from startfunc or from some function
| created by it.  And so on.
|
| So, if one could keep track of a "function creation history" you can
| detect when a dynamic binary is no longer in use.  Luckily that's easy *g*
| You just keep a reference to the binary in the upvalues:
|
|  - the change to Lua's core: lua_pushcclosure always appends the last
|    upvalue of the currently active cclosure to the new cclosure (or nil
|    if there is no active cclosure).
|
|  - loadlib creates a userdata containing the handle for that binary.
|    A GC call of the userdata will unload the binary.
|
|  - loadlib attaches that userdata as the single upvalue for the created
|    cclosure of startfunc.  (Needs a special pushcclosure or a magic
|    nupvalue so that lua_pushcclosure will not do its append stuff.)
|
| That's all.  Automatic garbage collection of dynamic loaded binaries.

Ciao, ET.

PS: Btw, what happens under Windows when an application tries to load a dll
    multiple times?  Is it allowed?  How many FreeLibrary-calls are needed?
    Does each instance gets its own data/bss segment?

[1] loadlib"io"  f=io.open"foo"  io=nil  GC()  print(f:read"*a")