I have the following Rust code, and I'm unsure why the returned address (raw_block
) is invalid:
let m = unsafe { &*(raw_data as *const Matrix<Double>) };
inverse(m).map(|inv| {
let raw_block = unsafe { alloc_zeroed(Layout::new::<Matrix<Double>>()) };
unsafe { *(raw_block as *mut Matrix<Double>) = inv };
(
Value::RawObject(String::from("Matrix"), raw_block),
Environment::new(),
)
})
This issue does not occur on Linux and macOS. Interestingly, if I use inv
in the following way:
let m = unsafe { &*(raw_data as *const Matrix<Double>) };
inverse(m).map(|inv| {
dbg!(&inv); // here
let raw_block = unsafe { alloc_zeroed(Layout::new::<Matrix<Double>>()) };
unsafe { *(raw_block as *mut Matrix<Double>) = inv };
(
Value::RawObject(String::from("Matrix"), raw_block),
Environment::new(),
)
})
the problem disappears, but this introduces a dbg!
print statement. Why does this happen, and is there a way to ensure inv
is valid without using dbg!
?
You can find the specific code in this repository rswk/ksl_matrix.
Steps to reproduce:
- Clone the repository to your local machine:
git clone https://github.com/kands-code/rswk
- Navigate to the local repository directory and build the
ksl
andksl_matrix
subprojects:cd rswk && cargo build -p ksl && cargo build -p ksl_matrix
- Install
ksl
:cargo install --path ksl --debug
- Copy the compiled library to the
ksl_matrix
directory:mkdir .\ksl_matrix\lib && cp .\target\debug\ksl_matrix.dll .\ksl_matrix\lib
- Change to the
ksl_matrix
directory and executeexample.ksl
:cd .\ksl_matrix && ksl example.ksl
Since this issue only occurs on Windows, the syntax used here is for Windows PowerShell. If you want to try obtaining the expected output on Linux or macOS, you need to replace
ksl_matrix.dll
with the correspondinglibksl_matrix.so
orlibksl_matrix.dylib
.
The expected output should be:
[1, 2, 1]
[2, 1, 0]
[-1, 1, 2]
[-0.6666666666666665, 1, 0.33333333333333326]
[1.3333333333333333, -1, -0.6666666666666666]
[-1, 1, 1]
()
Any insights on why this behavior occurs and how to resolve it would be greatly appreciated!