#Make a ptr live longer than the closure for .map()

8 messages · Page 1 of 1 (latest)

cosmic vigil
#

I'm working on something that interacts with LXC and currently I have problems with pointers (This is the first time I've really touched this stuff). Whenever I try turning an array of args (&[&str]) to a vector of pointers (Vec<*const i8>) it always drops them before I can actually use them. The reason is pretty obvious as .as_ptr() tells you, but I cant figure out a way to fix it.

steady coral
#

the thing that you need to not drop is the CStrings since they are what actually own the null-terminated strings

#

so, collect the CStrings and then, separately, iterate over that to collect the pointers to them

cosmic vigil
#

Wow, thanks

fading fiber
#

You could also call CString::into_raw

#

you'll have to remember to deallocate the CStrings after the function call though
(argv.into_iter().for_each(CString::from_raw);).
Not great compared to the other solution, if you don't mind the extra Vec for the CStrings.

elder juniper
#

first thing you're doing wrong is using vscode

#

💀