How can changing code that is not called break functionality?

Hi! One is for C++ experts:

This code is never called in my program. It would draw a few circles if it was.

std::vector<ofPolyline> loops;
ofFbo preview;

void LoopPreview::draw(u_long frm, const glm::vec2 & pos, const glm::vec2 & sz) {
    if(preview.isAllocated()) {
        ImDrawList * draw_list = ImGui::GetWindowDrawList();
        for(auto & loop : loops) {
            draw_list->AddCircleFilled(pos, 5, ImColor(255, 0, 255), 12);
        }
    }
}

The weird thing is, if I make a change to that unused code it breaks the whole GUI.

void LoopPreview::draw(u_long frm, const glm::vec2 & pos, const glm::vec2 & sz) {
    if(preview.isAllocated()) {
        ImDrawList * draw_list = ImGui::GetWindowDrawList();
        for(auto & loop : loops) {
            // access vertices in the "loop" polyline 
            draw_list->AddCircleFilled(pos 
                                       + loop[static_cast<int>(frm)], // THIS BREAKS THE GUI
                                       5, ImColor(255, 0, 255), 12);
        }
    }
}

How can a change to an unused method break the program?

I did put breakpoints and print to the console to make sure the code is never executed.

I figured out a workaround.Replacing the loop works fine:

        for(auto & loop : loops) {
            auto & p = loop[static_cast<int>(frm)];
            glm::vec2 center {
                pos.x + p.x,
                pos.y + p.y
            };
            draw_list->AddCircleFilled(center, 5, ImColor(255, 0, 255), 12);
        }

Calling AddCircleFiled(pos + p, ...) does not work, where

  • pos is a const glm::vec2 &.
  • p is whatever is returned by using [] on a ofPolyline
  • The first argument expected by AddCircleFilled is a const ImVec2 &.

Maybe the issue is that the first argument for AddCircleFilled should be a reference? What I don’t get is why does it compile? Why does the program run and this unused code produce weird side effects on other parts of the program?

1 Like