I have a similar use case as Sydius but I'm having trouble understanding how the proposed rc_borrow
solution works. I have a static
global hashmap that stores signal handlers. In these signal handlers, I need to invoke a method on an object that I do not want to have a static
lifetime. (Otherwise, the requirement of a static
lifetime would percolate through the entire application, essentially rendering all of Rust's memory management meaningless. The way I understand it, I would end up with a huge memory leak, i.e., all allocated memory would persist until the end of the program.)
I was also hoping to create the link between the global static anchor and the lifetime managed objects using Rc::weak
or, if Rc::weak
is not suitable, using an alternate mechanism, but I'm unsure how to do this.
I'll sketch what I intend to do in pseudo C below:
weakref *global;
void signalhandler(void) {
if (strong = upgrade_weakref(global)) {
use(strong);
}
}
main() {
object * ns = new object(); // not `static
global = make_suitable_weakref(ns);
....
}
My program guarantees that no other code runs during the call to use(strong)
. I can also guarantee that use()
would not make copies of strong
itself that would keep references to the object referred to be global
(so I could work with an unsafe mechanism that makes these assumptions.) I'm mentioning this because the concern that was raised by CAD97, namely that the lifetime of the inner 'a
value is not known may not apply to my use case.