Fix: crash in PA calibration pattern generation #662

Closed
opened 2026-04-05 16:25:19 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @tome9111991 on 2/21/2026

Description

The Issue:
When generating a Pressure Advance (PA) calibration pattern with a larger number of test parameters (e.g., 3 acceleration
values and 4 speed values, resulting in 12 pattern objects), OrcaSlicer consistently crashes on Windows during the "Slice
Plate" phase.

Root Cause:
The crash is caused by a dangling pointer (Use-After-Free) in src/libslic3r/GCodeWriter.cpp.
During the PA pattern g-code generation, GCodeWriter::set_extruders is called repeatedly. This function clears and
repopulates the m_filament_extruders vector. However, it failed to reset m_curr_extruder_id and the
m_curr_filament_extruder pointer cache.
When the vector reallocates and moves its contents in memory, the cached pointers are left pointing to the freed memory
block. Later, when set_extruder() or retraction_length() is called, accessing these dangling pointers results in an Access
Violation on Windows.

Additionally, set_extruders called std::max_element(extruder_ids.begin(), extruder_ids.end()) without checking if the
vector is empty, which is also undefined behavior.

The Fix:

  • Modified GCodeWriter::set_extruders to explicitly reset m_curr_extruder_id = -1 and clear the m_curr_filament_extruder
    array (nullptr) whenever the extruder list is rebuilt. This correctly invalidates the cache and forces the writer to
    properly fetch valid pointers on the next toolchange/extruder initialization.
  • Added a safety check (!extruder_ids.empty()) before calling std::max_element to prevent potential undefined behavior
    with empty vectors.

Testing Performed:

  • Verified that generating a PA pattern with 12 combinations (Accel: 1000, 2000, 4000 / Speed: 50, 100, 150, 200) now
    slices successfully on Windows without crashing.

VS2022 Crash:
grafik

*Originally created by @tome9111991 on 2/21/2026* # Description **The Issue:** When generating a Pressure Advance (PA) calibration pattern with a larger number of test parameters (e.g., 3 acceleration values and 4 speed values, resulting in 12 pattern objects), OrcaSlicer consistently crashes on Windows during the "Slice Plate" phase. **Root Cause:** The crash is caused by a dangling pointer (Use-After-Free) in `src/libslic3r/GCodeWriter.cpp`. During the PA pattern g-code generation, `GCodeWriter::set_extruders` is called repeatedly. This function clears and repopulates the `m_filament_extruders vector`. However, it failed to reset `m_curr_extruder_id` and the `m_curr_filament_extruder` pointer cache. When the vector reallocates and moves its contents in memory, the cached pointers are left pointing to the freed memory block. Later, when `set_extruder()` or `retraction_length()` is called, accessing these dangling pointers results in an Access Violation on Windows. Additionally, `set_extruders called std::max_element(extruder_ids.begin(), extruder_ids.end())` without checking if the vector is empty, which is also undefined behavior. **The Fix:** * Modified `GCodeWriter::set_extruders` to explicitly reset `m_curr_extruder_id = -1` and clear the `m_curr_filament_extruder` array (nullptr) whenever the extruder list is rebuilt. This correctly invalidates the cache and forces the writer to properly fetch valid pointers on the next toolchange/extruder initialization. * Added a safety check `(!extruder_ids.empty())` before calling `std::max_element` to prevent potential undefined behavior with empty vectors. **Testing Performed:** * Verified that generating a PA pattern with 12 combinations (Accel: 1000, 2000, 4000 / Speed: 50, 100, 150, 200) now slices successfully on Windows without crashing. VS2022 Crash: <img width="1887" height="864" alt="grafik" src="https://github.com/user-attachments/assets/7647bf39-85ef-480d-8a2d-3239fe28ae95" />
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/OrcaSlicer#662