enkisoftware / imgui_markdown

Markdown for Dear ImGui
zlib License
1.03k stars 69 forks source link

Example segfaults #19

Closed flamendless closed 3 years ago

flamendless commented 3 years ago

Hi, im trying this library for my project but when i call the ImGui::Markdown(...) it segfaults. here's a snippet of my code (with few modifications as i dont need image and links in markdown so i've removed the corresponding callbacks)

#include "imgui.h"
#include "imgui_markdown.h"
#include "IconsFontAwesome5.h"
//font.cpp
//the following are static variables declared in font.hpp
ImFont* Font::H1;
ImFont* Font::H2;
ImFont* Font::H3;
ImGui::MarkdownConfig Font::md_config;
const char* Font::connected_path = "assets/fonts/test.ttf";

//font loading
const float md_font_size = 12.0f;
ImGuiIO& io = ImGui::GetIO();
// io.Fonts->Clear(); //not called as i load many fonts prior to loading fonts for markdown
io.Fonts->AddFontFromFileTTF(Font::connected_path, md_font_size);
Font::H2 = io.Fonts->AddFontFromFileTTF(Font::connected_path, md_font_size);
Font::H3 = md_config.headingFormats[1].font;
Font::H1 = io.Fonts->AddFontFromFileTTF(Font::connected_path, md_font_size * 1.1f);

//help.cpp
void md_format_cb(const ImGui::MarkdownFormatInfo& md_info, bool start)
{
    ImGui::defaultMarkdownFormatCallback(md_info, start);
}

void Help::markdown(const std::string& str_md)
{
    Font::md_config.linkCallback = NULL;
    Font::md_config.tooltipCallback = NULL;
    Font::md_config.imageCallback = NULL;
    Font::md_config.linkIcon = NULL;
    Font::md_config.headingFormats[0] = { Font::H1, true };
    Font::md_config.headingFormats[1] = { Font::H2, true };
    Font::md_config.headingFormats[2] = { Font::H3, false };
    Font::md_config.userData = NULL;
    Font::md_config.formatCallback = md_format_cb;
    ImGui::Markdown(str_md.c_str(), str_md.length(), Font::md_config); //segfaults
}

//call
const std::string str = u8R"(
        # H1
        ## H2
        ### H3
)";
Help::markdown(str);

Backend: opengl2 and glfw DearImGui Version: 1.81 WIP

flamendless commented 3 years ago

Found out that i need to call the function inside a imgui call so there's context.