为什么Valgrind使用每晚1.29.0无法在Rust程序中检测到内存泄漏?

时间:2018-07-25 00:33:08

标签: rust valgrind

我正在尝试在this blog post之后使用Valgrind检测Rust程序中的内存泄漏。我的源代码很简单:

from xlrd import open_workbook
from xlutils.copy import copy

rb = open_workbook('C:\\Users\\Eric\\Desktop\\test.csv')
wb = copy(rb)
sheet = rb.sheet_by_index(0)
sheet.cell_value(0, 0)

first_row_list = sheet.row_values(0)
name_index = first_row_list.index('Name') 
print(name_index)

for i in range(sheet.nrows):
    target_column = sheet.cell_value(i, name_index)
    print(i)
    target_column.replace("a", "b")
    print(target_column.replace("a", "b"))
wb.save('C:\\Users\\Eric\\Desktop\\test.csv'`

我希望对#![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(); } 的调用会产生内存泄漏,Valgrind将能够解决该泄漏。但是,当我运行Valgrind时,它报告没有泄漏:

mem::forget()

我已升级到最新的每晚(每晚1.29.0晚上(6a1c0637c 2018-07-23))。

我想念什么?

1 个答案:

答案 0 :(得分:10)

铁锈1.32

从Rust 1.32开始,可执行文件的默认分配器now the system allocator,因此您无需在默认情况下进行任何设置。

以前的版本

您没有正确使用全局分配器设置。这是一个 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)