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

ffi/CString: Note that with system allocator, C free() is safe #56295

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,17 @@ impl CString {

/// Consumes the `CString` and transfers ownership of the string to a C caller.
///
/// The pointer which this function returns must be returned to Rust and reconstituted using
/// [`from_raw`] to be properly deallocated. Specifically, one
/// should *not* use the standard C `free()` function to deallocate
/// this string.
/// If the system allocator is in use, it is safe to have C invoke the corresponding
/// system-level deallocation function (e.g. `free()` on Unix, `HeapFree` on Windows)
/// on the returned pointer.
///
/// Failure to call [`from_raw`] will lead to a memory leak.
/// If you are using a custom allocator, then this value should be returned
/// to Rust and reconstituted using [`from_raw`] to be properly deallocated.
///
/// As a general rule, best practice is for library crates to not make an assumption
/// about the global allocator.
///
/// Failure to call [`from_raw`] (or `free()`) will lead to a memory leak.
///
/// [`from_raw`]: #method.from_raw
///
Expand Down