Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit c1e8600

Browse files
authored
am: Fix too much file handle cause memory leak
1 parent 7d00f47 commit c1e8600

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/core/hle/service/am/am.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ Result CIAFile::WriteTitleMetadata() {
194194
// TODO: Correct error code.
195195
return FileSys::ResultFileNotFound;
196196
}
197+
if (content_count > 255)
198+
file.Close();
197199
}
198200

199201
if (container.GetTitleMetadata().HasEncryptedContent()) {
@@ -224,6 +226,8 @@ ResultVal<std::size_t> CIAFile::WriteContentData(u64 offset, std::size_t length,
224226
// has been written since we might get a written buffer which contains multiple .app
225227
// contents or only part of a larger .app's contents.
226228
const u64 offset_max = offset + length;
229+
if (content_written.size() > 255)
230+
content_files.clear();
227231
for (std::size_t i = 0; i < content_written.size(); i++) {
228232
if (content_written[i] < container.GetContentSize(i)) {
229233
// The size, minimum unwritten offset, and maximum unwritten offset of this content
@@ -243,7 +247,9 @@ ResultVal<std::size_t> CIAFile::WriteContentData(u64 offset, std::size_t length,
243247
// Since the incoming TMD has already been written, we can use GetTitleContentPath
244248
// to get the content paths to write to.
245249
FileSys::TitleMetadata tmd = container.GetTitleMetadata();
246-
auto& file = content_files[i];
250+
auto path = GetTitleContentPath(media_type, tmd.GetTitleID(), i, is_update);
251+
auto& file = content_written.size() > 255 ? content_files.emplace_back(path, "ab+")
252+
: content_files[i];
247253

248254
std::vector<u8> temp(buffer + (range_min - offset),
249255
buffer + (range_min - offset) + available_to_write);
@@ -259,6 +265,8 @@ ResultVal<std::size_t> CIAFile::WriteContentData(u64 offset, std::size_t length,
259265
content_written[i] += available_to_write;
260266
LOG_DEBUG(Service_AM, "Wrote {:x} to content {}, total {:x}", available_to_write, i,
261267
content_written[i]);
268+
if (content_written.size() > 255)
269+
file.Close();
262270
}
263271
}
264272

0 commit comments

Comments
 (0)