4 ms·
I'd say the problem is in the documentation (or lack thereof.) I've had to use OpenSSL before and the question of "should this thing be freed after I use it?" w
by userbinator 12y ago
I'd say the problem is in the documentation (or lack thereof.) I've had to use OpenSSL before and the question of "should this thing be freed after I use it?" was very common and only resolved fully by inspecting the source carefully. The heavy use of macros adds to the frustration; often, 3 or more layers of them hide the actual code that gets executed, making it hard to tell where things go.
On the other hand, I've also worked on some codebases that were very well-documented with respect to this: every function that either returned a pointer or accepted pointers made it clear if/when they should be freed (and if so, with what function.) One example of that looks like:
void /* OWN */ *some_func(void *a, void /* OWN */ *b, void */* OWN */*c);
And the "/* OWN */" are intended to be read like "const" and "static", so this says the function returns a pointer to an object whose ownership is transferred to the caller, its first parameter does not change owners, the second one does, and the third is a pointer to an array of pointers whose ownership is passed to the function. The accompanying comment then says what function should be used to free the returned pointer, and what function will be used (by other code) to eventually free b and c.
- maemre 12y agoI know that proposing Rust in this case almost became a cliché but Rust's borrowing mechanism introduces such a semantic and ensures correctness in compile time. Moreover, with RAII it can also zero the memory after object is out of scope.
- doomrobo 12y agoWell, only for Box<> pointers, but yes
- masklinn 12y agoOwnership is for all structures on the stack. Box is little more than a rust-level wrapper around a raw pointer (well technically around a core::ptr::Unique which is the actual wrapper around a raw pointer)
- dbaupp 12y agoWhich part are you referring to with "only"? There's nothing particularly special about Box, at least, not in relation to the things the parent mentioned.
- doomrobo 12y agoWhoops, misread "zero" as "deallocate". I meant that Box<> pointers are heap-allocated but deallocated when they go out of scope.
- mtanski 12y agoExcept when escape out to unsafe (as a lot of rust code does) or stop using rust because the borrow checker is brain dead: https://github.com/rust-lang/rust/issues/6268 https://github.com/rust-lang/rust/issues/6268 https://github.com/rust-lang/rust/issues/6393 https://github.com/rust-lang/rust/issues/6393 Those bugs are 2 years old. And heaven forbid you have &mut self in a object.
- acveilleux 12y agoWhy not define proper keywords for this? #define own Then you don't need ugly comments... void own *some_func(void *a, void own *b, void * own *c);