Skip to content

lora_models() produces dangling pointers after build() due to Clone on LoraStorage #84

@khcd

Description

@khcd

Firstly I do not claim to be a rust or pointer expert this was just a bug I faced when using this library and I investigated further with Opus 4.6.

1 — api.rs:595 — raw pointer created from c_path:

  let c_path = CLibPath::from(path);        // allocates heap buffer at address 0xABC
  let lora = sd_lora_t {                                                                                                                                                                                                                
      path: c_path.as_ptr(),                 // stores raw pointer → 0xABC                                                                                                                                                              
  };

2 — api.rs:656 — both stored together in builder (still fine):

  self.lora_models = Some(LoraStorage {
      data,      // owns CLibPath (heap buffer at 0xABC)
      loras_t,   // sd_lora_t.path = 0xABC ✓
  });

3 — build() auto-generated by derive_builder — the clone:

  // derive_builder generates something like:
  lora_models: self.lora_models.clone()
  //           ^^^^^^^^^^^^^^^^^^^^^^^^
  //  Clone produces:
  //    data    → NEW CLibPath (heap buffer at 0xDEF)
  //    loras_t → sd_lora_t.path = 0xABC  ← still points to ORIGINAL

Then the builder drops → heap at 0xABC is freed → sd_lora_t.path is dangling.

The integration code on my side

base model works:

    let mut builder = ModelConfigBuilder::default();
    builder.model(path);

lora doesn't work:
builder.lora_models(&dir_path, specs) — but the pointer was dangling after build()
pointer dangles after build, resulting in:

The LoRA path sd.cpp receives is the temp output directory, not the lora directory. It should be /Users/user/.../loras/pixel-art-xl-v1.1.safetensors but instead it's the temp dir /var/folders/.../T/.tmpUvEJGV.

platform: OSX metal
diffusion-rs version: 0.1.18

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions