cargo-valgrind
A cargo subcommand, that runs valgrind and collects its output in a helpful manner.
This command extends cargo with the capability to directly run valgrind
on the executable (either a bin-crate or an example).
The output of valgrind is then used to mark the binary as pass/fail.
This command should not be necessary for ordinary Rust programs, especially if you are only using safe Rust code. But if you do FFI-related stuff (either by simply using a FFI-binding crate or because you are developing a safe wrapper for such FFI bindings) it may be really helpful to check, whether the memory usages across the FFI borders are correct.
A typical mistake would be: ```rust use std::ffi::CString; use std::os::raw::c_char;
extern "C" { fn puts(s: *const c_char); }
fn main() { let string = CString::new("Test").unwrap();
let ptr = string.into_raw();
unsafe { puts(ptr) };
// unsafe { CString::from_raw(ptr) };
}
The memory of the variable `string` will never be freed.
`cargo valgrind` detects it:
bash
$ cargo valgrind
Finished dev [unoptimized + debuginfo] target(s) in 0.01s
Analyzing target/debug/cstring
Test
Error Leaked 5 bytes
Info at realloc (vgreplacemalloc.c:826)
at realloc (alloc.rs:125)
at realloc (alloc.rs:184)
at reserveinternal
Un-commenting the
unsafe { CString::from_raw(ptr) };re-takes the memory and frees it correctly.
cargo valgrind` will compile the binary for you and won't detect a leak, since there is no leak anymore.
You need to have valgrind
installed and in the PATH
(you can test this by running valgrind --help
in your shell).
You'll also need to have cargo
installed and in the PATH
, but since this is a cargo subcommand, you will almost certainly have it already installed.
Run the following command:
cargo install --git https://github.com/jfrimmel/cargo-valgrind