Why does Valgrind not detect a memory leak in a Rust program using nightly 1.29.0?

Rust 1.32

As of Rust 1.32, the default allocator for an executable is now the system allocator, so you don’t need to set anything by default.

Previous versions

You aren’t using the global allocator setting correctly. This is a nightly feature, which means that it’s prone to change at any time. Your blog post is out of date.

Check the module docs for std::alloc to see the correct usage:

#![feature(alloc_system)]
extern crate alloc_system;

#[global_allocator]
static GLOBAL: alloc_system::System = alloc_system::System;

use std::mem;

fn allocate() {
    let bad_vec = vec![0u8; 1024*1024];
    mem::forget(bad_vec);
}

fn main() {
    allocate();
}
root@3fb431791293:/tmp/vg# valgrind target/debug/vg
==6326== Memcheck, a memory error detector
==6326== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6326== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==6326== Command: target/debug/vg
==6326==
==6326==
==6326== HEAP SUMMARY:
==6326==     in use at exit: 1,048,576 bytes in 1 blocks
==6326==   total heap usage: 12 allocs, 11 frees, 1,050,753 bytes allocated
==6326==
==6326== LEAK SUMMARY:
==6326==    definitely lost: 1,048,576 bytes in 1 blocks
==6326==    indirectly lost: 0 bytes in 0 blocks
==6326==      possibly lost: 0 bytes in 0 blocks
==6326==    still reachable: 0 bytes in 0 blocks
==6326==         suppressed: 0 bytes in 0 blocks
==6326== Rerun with --leak-check=full to see details of leaked memory
==6326==
==6326== For counts of detected and suppressed errors, rerun with: -v
==6326== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Leave a Comment