AutomaticComponentToolkit icon indicating copy to clipboard operation
AutomaticComponentToolkit copied to clipboard

[cgo] unloadLibrary is never called

Open alexanderoster opened this issue 4 years ago • 3 comments

We would need to call unload library with the wrapper destructor, but I am a bit helpless how this works. @qmuntal: Is there a chance you can take a look at this?

alexanderoster avatar Jul 28 '20 08:07 alexanderoster

I don´t think it make sense to add the auto-release capability to the wrapper. It clearly a resource loaded by the user, therefore it should be unloaded by the user, same as when opening/closing files or http requests.

I would just add a Unload method to the Wrapper struct that performs the unloading.

qmuntal avatar Jul 28 '20 08:07 qmuntal

I see, thank you for the feedback! But does that mean the unload will leak memory if the wrapper goes out of scope?

alexanderoster avatar Jul 28 '20 11:07 alexanderoster

But does that mean the unload will leak memory if the wrapper goes out of scope?

Yes, unless the user calls unload before the wrapper goes out of scope. defer is very helpful in this scenarios:

lib := act.LoadLibrary("C:/foo.dll")
defer lib.Unload() // lib will be automatically released when going out of scope

qmuntal avatar Jul 28 '20 13:07 qmuntal