Possible bug in TabBarGetCurrentTab #7629

Closed
opened 2024-05-27 16:31:36 +00:00 by VerySmallRoach · 0 comments
VerySmallRoach commented 2024-05-27 16:31:36 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

master

Back-ends:

imgui_impl_dx11.cpp + imgui_impl_win32.cpp

Compiler, OS:

Win 10 + MSVC 2022

Full config/build information:

No response

Details:

I believe there's a typo in TabBarGetCurrentTab:

if (tab_bar->LastTabItemIdx <= 0 || tab_bar->LastTabItemIdx >= tab_bar->Tabs.Size)
    return NULL;
return &tab_bar->Tabs[tab_bar->LastTabItemIdx];

First condition should be tab_bar->LastTabItemIdx < 0, otherwise it will return NULL for the first tab.
In attached example selecting Tab2 or Tab3 prints meaningful address, but selecting Tab1 prints zeroes.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

if (ImGui::Begin("Test")) {
    if (ImGui::BeginTabBar("TabBar")) {
        ImGuiTabBar* tabBar = ImGui::GetCurrentTabBar();
        if (ImGui::BeginTabItem("Tab1")) {
            std::cout << "tab1 " << ImGui::TabBarGetCurrentTab(tabBar) << std::endl;
            ImGui::EndTabItem();
        }
        if (ImGui::BeginTabItem("Tab2")) {
            std::cout << "tab2 " << ImGui::TabBarGetCurrentTab(tabBar) << std::endl;
            ImGui::EndTabItem();
        }
        if (ImGui::BeginTabItem("Tab3")) {
            std::cout << "tab3 " << ImGui::TabBarGetCurrentTab(tabBar) << std::endl;
            ImGui::EndTabItem();
        }
        ImGui::EndTabBar();
    }
}
ImGui::End();
### Version/Branch of Dear ImGui: master ### Back-ends: imgui_impl_dx11.cpp + imgui_impl_win32.cpp ### Compiler, OS: Win 10 + MSVC 2022 ### Full config/build information: _No response_ ### Details: I believe there's a typo in TabBarGetCurrentTab: ``` if (tab_bar->LastTabItemIdx <= 0 || tab_bar->LastTabItemIdx >= tab_bar->Tabs.Size) return NULL; return &tab_bar->Tabs[tab_bar->LastTabItemIdx]; ``` First condition should be `tab_bar->LastTabItemIdx < 0`, otherwise it will return NULL for the first tab. In attached example selecting Tab2 or Tab3 prints meaningful address, but selecting Tab1 prints zeroes. ### Screenshots/Video: _No response_ ### Minimal, Complete and Verifiable Example code: ```cpp if (ImGui::Begin("Test")) { if (ImGui::BeginTabBar("TabBar")) { ImGuiTabBar* tabBar = ImGui::GetCurrentTabBar(); if (ImGui::BeginTabItem("Tab1")) { std::cout << "tab1 " << ImGui::TabBarGetCurrentTab(tabBar) << std::endl; ImGui::EndTabItem(); } if (ImGui::BeginTabItem("Tab2")) { std::cout << "tab2 " << ImGui::TabBarGetCurrentTab(tabBar) << std::endl; ImGui::EndTabItem(); } if (ImGui::BeginTabItem("Tab3")) { std::cout << "tab3 " << ImGui::TabBarGetCurrentTab(tabBar) << std::endl; ImGui::EndTabItem(); } ImGui::EndTabBar(); } } ImGui::End(); ```
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#7629
No description provided.