pharo icon indicating copy to clipboard operation
pharo copied to clipboard

[Feature Request] Unload Library in Windows environment

Open ELePors opened this issue 1 year ago • 11 comments

In pharo, you can use FFI to call library functions. I would like a way to reload a C library in Pharo without restarting the VM. On windows for the moment but it will be necessary in other OS like Linux or MacOs.

Because the VM is a UI application an restarting the UI will be not acceptable by the user each time.

Describe the solution you'd like a unloadLibrary: in the FFILibrary class for example ?

or a reloadLibrary: should be used.

Thanks by advance for discussions on this topic This is a major improvement in our current library test environment

ELePors avatar Jan 31 '24 14:01 ELePors

Hi eric can you explain the why you want to reload a library? What is your scenario?

S

Ducasse avatar Jan 31 '24 16:01 Ducasse

On Windows you can use

BOOL FreeLibrary(
  [in] HMODULE hLibModule
);

https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-freelibrary

Frees the loaded dynamic-link library (DLL) module and, if necessary, decrements its reference count.

astares avatar Jan 31 '24 23:01 astares

I have some libraries which I have which don't be able to restart their context... it is internal one shot function in library called by a command line program... I just want to reuse the function ... and if I start the function 2 times ... it crashes... when I reload the lib with other C binding (python or ... ) no crash... I just reload, and restart to call the function another time. (releasing the memory before to avoir memory leak... )

ELePors avatar Jan 31 '24 23:01 ELePors

i will try to implement it ... or it is present in OS-Windows ?

ELePors avatar Jan 31 '24 23:01 ELePors

I agree this could be nice to have.

Just be careful that unloading the lib is only part of the deal :) We need also to flush/regenerate all the loaded functions cached in the (already called) ffi methods for that library.

guillep avatar Feb 01 '24 08:02 guillep

Ok, i should try a pull request for Windows Environment doing a unloadLibrary: aLib calling the FFI on the function and regenerating the cached ffi functions of that library ?

ELePors avatar Feb 02 '24 09:02 ELePors

Is there a way to find all ffiCall on a FFILibrary from the ffi cache ? It should be very usefull to reload a library completely. I succeeded in reloading a Windows library... but in order to do that : "We need also to flush/regenerate all the loaded functions cached in the (already called) ffi methods for that library"... i need to know ! :)

ELePors avatar Feb 15 '24 10:02 ELePors

Hi Eric, you can do for a class:

FFIMethodRegistry uniqueInstance resetSingleClass: aClass 

or for a method

FFIMethodRegistry uniqueInstance removeMethod: aMethod 

Cheers, Pablo

tesonep avatar Feb 15 '24 10:02 tesonep

Very nice... i've just found it !

ELePors avatar Feb 15 '24 10:02 ELePors

I proposed a solution in OS-Windows . Maybe Pharo should have a portable solution for every OS...

Take a look to WinLibrary ... i think it can be enhanced and generalized... Do we close the issue ?

ELePors avatar Feb 16 '24 08:02 ELePors

We would like but this is a problem of manpower. For example for calling OS function.

Ducasse avatar Feb 16 '24 10:02 Ducasse