Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to store loaded function #155

Closed
zxcqirara opened this issue Aug 12, 2024 · 6 comments
Closed

Trying to store loaded function #155

zxcqirara opened this issue Aug 12, 2024 · 6 comments

Comments

@zxcqirara
Copy link

I want to store function somewhere to invoke it later in the code but every time I try to do it, rust throws STATUS_ACCESS_VIOLATION error. If I store Symbol<fn()> then it says that Library "does not live long enough"

@nagisa
Copy link
Owner

nagisa commented Aug 13, 2024

Well, the last sentence does explain it all, doesn't it? Library needs to live for as long as you want to use your Symbols. If not crash, what other well defined things could possibly happen when working with functions from an unloaded library?

There are plenty of different approaches to ensure that Library lives for long enough: one of them being to leak the Library, either by using appropriate flags at the time Library is loaded or by mem::forgeting the Library once you've obtained your function pointers out of it. At that point you can store the function pointer wherever as if it the underlying code had a 'static lifetime.

The ecosystem also has some options that combine a Library and its symbols into a single struct, so you don't need to worry about lifetimes, while still ensuring proper clean-up. For instance, the bindgen crate can generate something for you here (or you could draw inspiration from it's implementation...)

@nagisa
Copy link
Owner

nagisa commented Aug 13, 2024

For what it is worth, some of the issues you've encountered are related to #46, which is unfortunately still isn't easily solvable in the present-day Rust (due to fn() function pointers implying the 'static lifetime.)

@zxcqirara
Copy link
Author

There are plenty of different approaches to ensure that Library lives for long enough: one of them being to leak the Library, either by using appropriate flags at the time Library is loaded or by mem::forgeting the Library once you've obtained your function pointers out of it. At that point you can store the function pointer wherever as if it the underlying code had a 'static lifetime.

Can you please explain your thought about flags? I didn't really work with such things before, so sorry about this question

@nagisa
Copy link
Owner

nagisa commented Aug 14, 2024

Some of the targets support options to loading functions like Linux's RTLD_NODELETE which will prevent the library from being unloaded when a function to unload the dynamic library is invoked. libloading::os APIs provide an ability to pass such flags through. Not sure if there's such a flag for Windows, though, so mem::forget is a more sure-fire method here.

@zxcqirara
Copy link
Author

I have already realized it via the mem::forget, but thank you anyway!

@zxcqirara
Copy link
Author

ig I can close the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants