Skip to content

Commit

Permalink
Fixes memory leak in DisplayList resource. (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenix3 committed May 9, 2024
1 parent 9fd2fa8 commit 0da318c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/resource/factory/DisplayListFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ std::shared_ptr<Ship::IResource> ResourceFactoryXMLDisplayListV0::ReadResource(s

g.words.w0 &= 0x00FFFFFF;
g.words.w0 += (G_MTX_OTR2 << 24);
g.words.w1 = (uintptr_t)malloc(fName.size() + 1);
char* str = (char*)malloc(fName.size() + 1);
g.words.w1 = (uintptr_t)str;
dl->Strings.push_back(str);
strcpy((char*)g.words.w1, fName.data());
}
} else if (childName == "SetCycleType") {
Expand Down Expand Up @@ -395,10 +397,11 @@ std::shared_ptr<Ship::IResource> ResourceFactoryXMLDisplayListV0::ReadResource(s
std::string fName = child->Attribute("Path");
// fName = ">" + fName;

char* filePath = (char*)malloc(fName.size() + 1);
strcpy(filePath, fName.data());
char* str = (char*)malloc(fName.size() + 1);
dl->Strings.push_back(str);
strcpy((char*)str, fName.data());

g = GsSpVertexOtR2P1(filePath);
g = GsSpVertexOtR2P1(str);

dl->Instructions.push_back(g);

Expand Down Expand Up @@ -450,7 +453,9 @@ std::shared_ptr<Ship::IResource> ResourceFactoryXMLDisplayListV0::ReadResource(s
g = { gsDPSetTextureImage(fmtVal, sizVal, width + 1, 0) };
g.words.w0 &= 0x00FFFFFF;
g.words.w0 += (G_SETTIMG_OTR_FILEPATH << 24);
g.words.w1 = (uintptr_t)malloc(fName.size() + 1);
char* str = (char*)malloc(fName.size() + 1);
dl->Strings.push_back(str);
g.words.w1 = (uintptr_t)str;
strcpy((char*)g.words.w1, fName.data());
}

Expand Down Expand Up @@ -893,7 +898,9 @@ std::shared_ptr<Ship::IResource> ResourceFactoryXMLDisplayListV0::ReadResource(s
g = { gsDPSetTextureImage(fmt, siz, width + 1, 0) };
g.words.w0 &= 0x00FFFFFF;
g.words.w0 += (G_SETTIMG_OTR_FILEPATH << 24);
g.words.w1 = (uintptr_t)malloc(fName.size() + 1);
char* str = (char*)malloc(fName.size() + 1);
dl->Strings.push_back(str);
g.words.w1 = (uintptr_t)str;
strcpy((char*)g.words.w1, fName.data());

dl->Instructions.push_back(g);
Expand Down Expand Up @@ -952,6 +959,7 @@ std::shared_ptr<Ship::IResource> ResourceFactoryXMLDisplayListV0::ReadResource(s
g = { gsSPBranchListOTRHash(seg | 1) };
} else {
char* dlPath2 = (char*)malloc(strlen(dlPath.c_str()) + 1);
dl->Strings.push_back(dlPath2);
strcpy(dlPath2, dlPath.c_str());

g = gsSPBranchListOTRFilePath(dlPath2);
Expand All @@ -963,6 +971,7 @@ std::shared_ptr<Ship::IResource> ResourceFactoryXMLDisplayListV0::ReadResource(s
g = { gsSPDisplayList(seg | 1) };
} else {
char* dlPath2 = (char*)malloc(strlen(dlPath.c_str()) + 1);
dl->Strings.push_back(dlPath2);
strcpy(dlPath2, dlPath.c_str());

g = gsSPDisplayListOTRFilePath(dlPath2);
Expand Down
7 changes: 7 additions & 0 deletions src/resource/type/DisplayList.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include "resource/type/DisplayList.h"
#include <memory>

namespace LUS {
DisplayList::DisplayList() : Resource(std::shared_ptr<Ship::ResourceInitData>()) {
}

DisplayList::~DisplayList() {
for (char* string : Strings) {
free(string);
}
}

Gfx* DisplayList::GetPointer() {
return (Gfx*)Instructions.data();
}
Expand Down
2 changes: 2 additions & 0 deletions src/resource/type/DisplayList.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ class DisplayList : public Ship::Resource<Gfx> {
using Resource::Resource;

DisplayList();
~DisplayList();

Gfx* GetPointer() override;
size_t GetPointerSize() override;

std::vector<Gfx> Instructions;
std::vector<char*> Strings;
};
} // namespace LUS

0 comments on commit 0da318c

Please sign in to comment.