Why are my variables duplicated and sometimes corrupted when debugging?

Viewed 214

I have a very straightforward rust function which just simply wants to get the first value of the list that is passed into it.

use std::fmt::Debug;
use std::ops::Add;

fn main() {
    fn calculate<T:Debug + Add::<Output=T>, const N: usize>(data_set: [T; N]) -> (i32, i32) {
        // Key Code 
        let _var = data_set.get(0);
        println!("Output_1: {:?}", data_set.get(0)); 

        // Ignore
        return (0, 0)
    }

    let data = [1509, 1857, 1736, 1815, 1576];
    let result = calculate(data);
    println!("Output_2: {:?}", result);
}

I have been trying to debug it in VS Code with rust-analyser, but the local variables in the debug panel are being shown to be duplicated 3 times over which I don't understand why is happening.

Memory Stack/Pile/Heap before function:

data: {1509, 1857, 1736, 1815, 1576}

Memory Stack/Pile/Heap immediately after entering function:

data_set: {1509, 1857, 1736, 1815, 1576}
data_set: {1509, 1857, 1736, 1815, 1576}
data_set: {1509, 1857, 1736, 1815, 1576}

I have deduced that the problem is caused by the line println!("Output_1: {:?}", data_set.get(0)); because by removing it, the duplicate data_set's disappear. However, I frankly have no understanding why it causes the problem.

Furthermore, whilst executing the line println!("Output_1: {:?}", data_set.get(0));, the values go random and extreme, changing the memory stack/heap to

data_set: {1509, 1857, 1736, 1815, 1576}
data_set: {-445645856, 223, -445645856, 223, -445645856} // numbers are different each execution.
data_set: {1509, 1857, 1736, 1815, 1576}

Can someone explain why this happens?

1 Answers

This is a bug in the LLDB debugger that seems to have been introduced in version 1.6.9. I have filed an issue to address this behavior.

As a workaround, you can roll back the version to version 1.6.8 or earlier (or it appears to have since been fixed so you should update to a newer version).

Related