Fix some memory leaks with WrappedText and LoadArrayByNameAsVec3s (#4144)

* avoid memory leaks with WrappedText

* avoid memory leak with LoadArrayByNameAsVec3s
This commit is contained in:
Archez 2024-05-09 21:39:13 -04:00 committed by GitHub
parent cbeec006ec
commit 3f67fed073
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 14 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

@ -1735,6 +1735,7 @@ extern "C" char* ResourceMgr_LoadArrayByName(const char* path)
return (char*)res->Scalars.data();
}
// Return of LoadArrayByNameAsVec3s must be freed by the caller
extern "C" char* ResourceMgr_LoadArrayByNameAsVec3s(const char* path) {
auto res = std::static_pointer_cast<LUS::Array>(GetResourceByNameHandlingMQ(path));

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);

View File

@ -2307,10 +2307,12 @@ void Player_DrawPause(PlayState* play, u8* segment, SkelAnime* skelAnime, Vec3f*
}
srcTable = ResourceMgr_LoadArrayByNameAsVec3s(srcTable);
Vec3s* ogSrcTable = srcTable;
destTable = skelAnime->jointTable;
for (i = 0; i < skelAnime->limbCount; i++) {
*destTable++ = *srcTable++;
}
free(ogSrcTable);
}