Skip to content

Segfault when calling module.to_string #661

Description

@wyj2006

Describe the Bug
segfault when calling module.to_string
exception occured at the end of LLVMString::drop on my machine and this does not always happen.

To Reproduce

use inkwell::builder::Builder;
use inkwell::context::Context;
use inkwell::module::Module;

struct CodeGen<'ctx> {
    context: &'ctx Context,
    module: Module<'ctx>,
    builder: Builder<'ctx>,
}

impl<'ctx> CodeGen<'ctx> {
    fn build(&self) {
        let i32_type = self.context.i32_type();
        let fn_type = i32_type.fn_type(&[], false);
        let function = self.module.add_function("main", fn_type, None);
        let basic_block = self.context.append_basic_block(function, "entry");

        self.builder.position_at_end(basic_block);

        self.builder
            .build_return(Some(&i32_type.const_int(0, false)))
            .unwrap();
    }
}

fn main() {
    let context = Context::create();
    let module = context.create_module("test");
    let codegen = CodeGen {
        context: &context,
        module,
        builder: context.create_builder(),
    };

    codegen.build();

    println!("{}", codegen.module.to_string());
}

Expected Behavior
the program should not segfault after calling module.to_string function

LLVM Version (please complete the following information):

  • LLVM Version: 21.1.8
  • Inkwell Branch Used: llvm21-1

Desktop (please complete the following information):

  • OS: Windows11

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions