avoid memory leaks with WrappedText

This commit is contained in:
Adam Bird 2024-05-09 01:41:10 -04:00
parent fd9cd9c5eb
commit c9b1631b56
3 changed files with 11 additions and 11 deletions

View File

@ -782,7 +782,7 @@ void DrawFlagTableArray16(const FlagTable& flagTable, uint16_t row, uint16_t& fl
ImGui::PopStyleColor();
if (ImGui::IsItemHovered() && hasDescription) {
ImGui::BeginTooltip();
ImGui::Text("%s", UIWidgets::WrappedText(flagTable.flagDescriptions.at(row * 16 + flagIndex), 60));
ImGui::Text("%s", UIWidgets::WrappedText(flagTable.flagDescriptions.at(row * 16 + flagIndex), 60).c_str());
ImGui::EndTooltip();
}
ImGui::PopID();

View File

@ -21,7 +21,7 @@ namespace UIWidgets {
// Automatically adds newlines to break up text longer than a specified number of characters
// Manually included newlines will still be respected and reset the line length
// If line is midword when it hits the limit, text should break at the last encountered space
char* WrappedText(const char* text, unsigned int charactersPerLine) {
std::string WrappedText(const char* text, unsigned int charactersPerLine) {
std::string newText(text);
const size_t tipLength = newText.length();
int lastSpace = -1;
@ -43,17 +43,17 @@ namespace UIWidgets {
currentLineLength++;
}
return strdup(newText.c_str());
return newText;
}
char* WrappedText(const std::string& text, unsigned int charactersPerLine) {
std::string WrappedText(const std::string& text, unsigned int charactersPerLine) {
return WrappedText(text.c_str(), charactersPerLine);
}
void SetLastItemHoverText(const std::string& text) {
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text("%s", WrappedText(text, 60));
ImGui::Text("%s", WrappedText(text, 60).c_str());
ImGui::EndTooltip();
}
}
@ -61,7 +61,7 @@ namespace UIWidgets {
void SetLastItemHoverText(const char* text) {
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text("%s", WrappedText(text, 60));
ImGui::Text("%s", WrappedText(text, 60).c_str());
ImGui::EndTooltip();
}
}
@ -72,7 +72,7 @@ namespace UIWidgets {
ImGui::TextColored(ImVec4(0.7f, 0.7f, 0.7f, 1.0f), "?");
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text("%s", WrappedText(text, 60));
ImGui::Text("%s", WrappedText(text, 60).c_str());
ImGui::EndTooltip();
}
}
@ -82,7 +82,7 @@ namespace UIWidgets {
ImGui::TextColored(ImVec4(0.7f, 0.7f, 0.7f, 1.0f), "?");
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text("%s", WrappedText(text, 60));
ImGui::Text("%s", WrappedText(text, 60).c_str());
ImGui::EndTooltip();
}
}
@ -92,7 +92,7 @@ namespace UIWidgets {
void Tooltip(const char* text) {
if (ImGui::IsItemHovered()) {
ImGui::SetTooltip("%s", WrappedText(text));
ImGui::SetTooltip("%s", WrappedText(text).c_str());
}
}

View File

@ -50,8 +50,8 @@ namespace UIWidgets {
constexpr float sliderButtonWidth = 30.0f;
#endif
char* WrappedText(const char* text, unsigned int charactersPerLine = 60);
char* WrappedText(const std::string& text, unsigned int charactersPerLine);
std::string WrappedText(const char* text, unsigned int charactersPerLine = 60);
std::string WrappedText(const std::string& text, unsigned int charactersPerLine);
void SetLastItemHoverText(const std::string& text);
void SetLastItemHoverText(const char* text);