pthom / hello_imgui

Hello, Dear ImGui: unleash your creativity in app development and prototyping
https://pthom.github.io/hello_imgui
MIT License
676 stars 103 forks source link

Custom Menu Assert: NULL pointer to window #118

Closed algotron closed 4 months ago

algotron commented 4 months ago

Custom Menu: Menu accesses a NULL window pointer —————————————————————————

From the ImGui Bundle, I inserted the following lines to make a custom menu: —————————————————————————————————

 runnerParams.appWindowParams.windowTitle = "Dear ImGui Bundle interactive manual";
  runnerParams.appWindowParams.windowGeometry.size = {1400, 950};

  // Menu bar
  runnerParams.imGuiWindowParams.showMenuBar = true;
  // for custom menu
  runnerParams.imGuiWindowParams.showMenu_App = false;                                   // modified
  runnerParams.imGuiWindowParams.showMenu_View = false;                                  // modified
  runnerParams.callbacks.ShowMenus = [&_appState]() {  // setup the custom menu call     // modified
    ShowAppMainMenuBar(_appState);                                                       // modified
  };                                                                                     // modified
  runnerParams.imGuiWindowParams.showStatusBar = true;
  runnerParams.imGuiWindowParams.showStatusBar = true;

I then added the following: ———————————————————————————

void ShowAppMainMenuBar(CAppState & AppState) {

  // the main menu
  // refer to the  the ImGui example menu function: void ShowMenu(RunnerParams & runnerParams)

  // Process the file i/o here. If there is file i/o, then do the file i/o and not the menu

  if (ImGui::BeginMainMenuBar()) {

    ShowFileMenu(AppState);       // show the process data import /export menu
    ShowAboutMenu(AppState);

    ImGui::EndMainMenuBar();
  } //end menu bar

  return;
}

void ShowAboutMenu(CAppState & AppState) {

  if (ImGui::BeginMenu("About##AboutMenu0")) { // File Menu
    ImGui::EndMenu();
  } // Menu item new
  return;
}

void ShowFileMenu(CAppState & AppState) {

  if (ImGui::BeginMenu("File##FileMenu0")) { // File Menu
    ImGui::EndMenu();
  } // Menu item new
  return;
}

CAppState is:

class CAppState {

public:
  bool saveCurrentLayout = false;
};

Result: ——————

When the example code is run without the custom menu inserted, it works fine and the window is defined

In the custom menu is defined, the ASSERT at line 7304 in imgui_widgets.cpp on the Line IM_ASSERT(!window->DC.MenuBarAppending);

Here is the code:

bool ImGui::BeginMenuBar()
{
    ImGuiWindow* window = GetCurrentWindow();
    if (window->SkipItems)
        return false;
    if (!(window->Flags & ImGuiWindowFlags_MenuBar))
        return false;

    IM_ASSERT(!window->DC.MenuBarAppending);
    BeginGroup(); // Backup position on layer 0 // FIXME: Misleading to use a group for that backup/restore
    PushID("##menubar");

CASE 1: ————— using the modifications above, the assert is caused by

IM_ASSERT(!window->DC.MenuBarAppending); where window->DC.MenuBarAppending == true

CASE 2 ———— where the same modifications were made (in a more complex app), the pointer to the window is NULL

What am I missing?

pthom commented 4 months ago

Please provide me with a complete minimum reproducible example that I can compile on my side. Also, please enclose the code blocks with ``` see markdown guide here: https://www.markdownguide.org/extended-syntax/#fenced-code-blocks

pthom commented 4 months ago

Okay, let me provide some hints.  Please learn a bit about Markdown. This will help you format your questions. It seems like you're trying to do it anyway (which is a good thing !), but knowing markdown will help a lot: see https://www.markdownguide.org/basic-syntax/

Then, you should learn about how to provide minimal reproducible examples when asking questions: look at https://stackoverflow.com/help/minimal-reproducible-example In short:

As far as the answer is concerned, look at the 4th line of the doc for ShowMenus here: https://pthom.github.io/hello_imgui/book/doc_params.html#runnercallbacks , which I reproduced below.

    // `ShowMenus`: Fill it with a function that will add ImGui menus by calling:
    //       ImGui::BeginMenu(...) / ImGui::MenuItem(...) / ImGui::EndMenu()
    //   Notes:
    //   * you do not need to call ImGui::BeginMenuBar and ImGui::EndMenuBar
    //   * Some default menus can be provided:
    //     see ImGuiWindowParams options:
    //         _showMenuBar, showMenu_App_QuitAbout, showMenu_View_
    VoidFunction ShowMenus = EmptyVoidFunction();    
algotron commented 4 months ago

Hello Pascal,

I apologize for not responding earlier to your email following up on my post. I was trying to link against .a files in the ImGui Bundle demo build, but it was not at all obvious how to do this . I think the probable pointer issue may have stemmed from that.

I since dowloaded the dear imgui demo project and am linking against the archive files in that build. Unfortunately, I have not had a chance to test the errant pointer yet as the rebuild is still in progress.

Thank-you for the very helpful email that you sent.

Yours truly,

Peter Walker

On Jul 22, 2024, at 4:21 PM, Pascal Thomet @.***> wrote:

Closed #118 https://github.com/pthom/hello_imgui/issues/118 as completed.

— Reply to this email directly, view it on GitHub https://github.com/pthom/hello_imgui/issues/118#event-13606449484, or unsubscribe https://github.com/notifications/unsubscribe-auth/A4IQUUIEESI2STUJJFCXQHLZNVSUTAVCNFSM6AAAAABKZQY4UCVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJTGYYDMNBUHE2DQNA. You are receiving this because you authored the thread.