Skip to content

Generated wrapper main leaks memory of args #649

@kkysen

Description

@kkysen

The generated wrapper main wrapping main_0 leaks the memory of args:

pub fn main() {
    let mut args: Vec<*mut libc::c_char> = Vec::new();
    for arg in ::std::env::args() {
        println!("{:?}", arg);
        args.push(
            ::std::ffi::CString::new(arg)
                .expect("Failed to convert argument into CString.")
                .into_raw(),
        );
    }
    args.push(::std::ptr::null_mut());
    unsafe {
        main_0(
            (args.len() - 1) as libc::c_int,
            args.as_mut_ptr() as *mut *mut libc::c_char,
        );
    }
}

We should either free it at the end by using CString::from_raw and dropping it, or by making args: Vec<CString> and then another args: Vec<*mut libc::c_char> that points to the memory owned by the first args. I think the latter approach is better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions