rust – 在OSX中进行动态加载的分段故障

我正在尝试在Rust中进行一些动态库加载.从动态加载的库函数传递大的Vec时,我遇到了分段错误.这是创建Vec< i32>的基本功能.指定大小的.如果Vec远大于8MB,程序会在OSX上遇到段错误.我在
linux上运行时没有遇到同样的问题,任何人都可以看看这个并告诉我,如果我在这里做错了吗?我正在运行这个:

$cargo build --release
$./target/release/linkfoo
8281
[1]    84253 segmentation fault  ./target/release/linkfoo

Cargo.toml

[package]
name = "linkfoo"
version = "0.1.0"
authors = ["Nate Mara <nathan.mara@kroger.com>"]

[dependencies]
libloading = "0.3.0"

[lib]
name = "foo"
crate-type = ["dylib"]

main.rs

extern crate libloading as lib;

fn main() {
    let dylib = lib::Library::new("./target/release/libfoo.dylib").expect("Failed to load library");
    let func = unsafe {
        let wrapped_func: lib::Symbol<fn(&[i32]) -> Vec<i32>> = dylib.get(b"alloc")
            .expect("Failed to load function");

        wrapped_func.into_raw()
    };

    let args = vec![8182];
    println!("{}", func(&args).len());
}

lib.rs

#[no_mangle]
pub fn alloc(args: &[i32]) -> Vec<i32> {
    let size = args[0] as usize;
    let mut mat = Vec::with_capacity(size);

    for _ in 0..size {
        mat.push(0);
    }

    mat
}

最佳答案 Rust使用系统分配器用于动态库,而jemalloc用于所有其他代码.加载器的这种差异导致了段错误,我能够通过将它添加到main.rs的顶部来修复它:

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