Skip to content

Commit 0da318c

Browse files
authored
Fixes memory leak in DisplayList resource. (Kenix3#575)
1 parent 9fd2fa8 commit 0da318c

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/resource/factory/DisplayListFactory.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ std::shared_ptr<Ship::IResource> ResourceFactoryXMLDisplayListV0::ReadResource(s
278278

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

398-
char* filePath = (char*)malloc(fName.size() + 1);
399-
strcpy(filePath, fName.data());
400+
char* str = (char*)malloc(fName.size() + 1);
401+
dl->Strings.push_back(str);
402+
strcpy((char*)str, fName.data());
400403

401-
g = GsSpVertexOtR2P1(filePath);
404+
g = GsSpVertexOtR2P1(str);
402405

403406
dl->Instructions.push_back(g);
404407

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

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

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

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

968977
g = gsSPDisplayListOTRFilePath(dlPath2);

src/resource/type/DisplayList.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#include "resource/type/DisplayList.h"
2+
#include <memory>
23

34
namespace LUS {
45
DisplayList::DisplayList() : Resource(std::shared_ptr<Ship::ResourceInitData>()) {
56
}
67

8+
DisplayList::~DisplayList() {
9+
for (char* string : Strings) {
10+
free(string);
11+
}
12+
}
13+
714
Gfx* DisplayList::GetPointer() {
815
return (Gfx*)Instructions.data();
916
}

src/resource/type/DisplayList.h

+2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ class DisplayList : public Ship::Resource<Gfx> {
1111
using Resource::Resource;
1212

1313
DisplayList();
14+
~DisplayList();
1415

1516
Gfx* GetPointer() override;
1617
size_t GetPointerSize() override;
1718

1819
std::vector<Gfx> Instructions;
20+
std::vector<char*> Strings;
1921
};
2022
} // namespace LUS

0 commit comments

Comments
 (0)