我正在try 使用Valgrind this blog post检测Rust 程序中的内存泄漏.我的源代码很简单:

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

use std::mem;

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

fn main() {
    allocate();
}

我预计对mem::forget()的调用会产生一个Valgrind能够发现的内存泄漏.然而,当我运行Valgrind时,它报告说不可能出现泄漏:

[memtest]> cargo run
   Compiling memtest v0.1.0 (file:///home/icarruthers/memtest)
    Finished dev [unoptimized + debuginfo] target(s) in 0.28s
     Running `target/debug/memtest`
[memtest]> valgrind target/debug/memtest
==18808== Memcheck, a memory error detector
==18808== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==18808== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==18808== Command: target/debug/memtest
==18808== 
==18808== 
==18808== HEAP SUMMARY:
==18808==     in use at exit: 0 bytes in 0 blocks
==18808==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==18808== 
==18808== All heap blocks were freed -- no leaks are possible
==18808== 
==18808== For counts of detected and suppressed errors, rerun with: -v
==18808== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我升级到最新的夜间版本(1.29.0-nightly(6a1c0637c 2018-07-23)).

我错过了什么?

推荐答案

Rust 1.32

在Rust 1.32中,可执行文件的default allocatornow the system allocator,因此默认情况下不需要设置任何内容.

Previous versions

您没有正确使用全局分配器设置.这是一个nightly的特性,这意味着它随时都可能改变.你的博客帖子已经过时了.

判断module docs for std::alloc以查看正确用法:

#![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)

Rust相关问答推荐

是否可以为`T:Copy`执行`T. clone`的测试

如何最好地并行化修改同一Rust向量的多个切片的代码?

两个相关特征的冲突实现错误

在执行其他工作的同时,从共享裁判后面的VEC中删除重复项

如何正确重新排列代码以绕过铁 rust 借入判断器?

将一个泛型类型转换为另一个泛型类型

通过RabbitMQ取消铁 rust 中长时间运行的人造丝任务的策略

不能在Rust中使用OpenGL绘制三角形

不同类型泛型的映射

你能在Rust中弃用一个属性吗?

Sized问题的动态调度迭代器Rust

如何从 rust 中的同一父目录导入文件

简单 TCP 服务器的连接由对等重置错误,mio 负载较小

如何从 x86_64 Mac 构建 M1 Mac?

如何判断服务器是否正确接收数据

如何将 Rust 字符串转换为 i8(c_char) 数组?

你能告诉我如何在 Rust 中使用定时器吗?

意外的正则表达式模式匹配

为什么可以从闭包中返回私有 struct

使用 rust-sqlx/tokio 时如何取消长时间运行的查询