Skip to content

Commit b921b9f

Browse files
committed
ffi/CString: Note that with system allocator, C free() is safe
This text originated with e20a6db which was in 2015 when Rust was strongly oriented towards jemalloc. We now support the system allocator, and it's the default. Returning strings from Rust to C is much more ergonomic if one can invoke `free()` directly, so let's make that clear.
1 parent aeff91d commit b921b9f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/libstd/ffi/c_str.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,17 @@ impl CString {
414414

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

0 commit comments

Comments
 (0)