[Scummvm-git-logs] scummvm master -> 5c8ac251d737020218f82406f7f483ee399d7a76

antoniou79 noreply at scummvm.org
Sat May 20 09:33:29 UTC 2023


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
5c8ac251d7 TOON: Plug mem leak from loading subs resource


Commit: 5c8ac251d737020218f82406f7f483ee399d7a76
    https://github.com/scummvm/scummvm/commit/5c8ac251d737020218f82406f7f483ee399d7a76
Author: antoniou79 (a.antoniou79 at gmail.com)
Date: 2023-05-20T12:32:17+03:00

Commit Message:
TOON: Plug mem leak from loading subs resource

This was also reported when running with aSan

Changed paths:
    engines/toon/resource.cpp
    engines/toon/subtitles.cpp


diff --git a/engines/toon/resource.cpp b/engines/toon/resource.cpp
index 6d1316ebbfe..6394ac41905 100644
--- a/engines/toon/resource.cpp
+++ b/engines/toon/resource.cpp
@@ -78,7 +78,7 @@ void Resources::addToCache(const Common::String &packName, const Common::String
 	_cacheSize += fileSize;
 
 	while (_cacheSize > MAX_CACHE_SIZE) {
-		CacheEntry *bestEntry = 0;
+		CacheEntry *bestEntry = nullptr;
 		for (Common::Array<CacheEntry *>::iterator entry = _resourceCache.begin(); entry != _resourceCache.end(); ++entry) {
 			if ((*entry)->_data) {
 				if (!bestEntry || ((*entry)->_age >= bestEntry->_age && (*entry)->_size >= bestEntry->_size)) {
@@ -90,7 +90,7 @@ void Resources::addToCache(const Common::String &packName, const Common::String
 			break;
 
 		free(bestEntry->_data);
-		bestEntry->_data = 0;
+		bestEntry->_data = nullptr;
 		_cacheSize -= bestEntry->_size;
 		debugC(5, kDebugResource, "Freed %s (%s) to reclaim %d bytes", bestEntry->_fileName.c_str(), bestEntry->_packName.c_str(), bestEntry->_size);
 	}
@@ -153,7 +153,7 @@ uint8 *Resources::getFileData(const Common::String &fileName, uint32 *fileSize)
 		Common::File file;
 		bool opened = file.open(fileName);
 		if (!opened)
-			return 0;
+			return nullptr;
 
 		*fileSize = file.size();
 		uint8 *memory = (uint8 *)new uint8[*fileSize];
@@ -164,7 +164,7 @@ uint8 *Resources::getFileData(const Common::String &fileName, uint32 *fileSize)
 	} else {
 
 		uint32 locFileSize = 0;
-		uint8 *locFileData = 0;
+		uint8 *locFileData = nullptr;
 
 		if (getFromCache(fileName, &locFileSize, &locFileData)) {
 			*fileSize = locFileSize;
@@ -180,7 +180,7 @@ uint8 *Resources::getFileData(const Common::String &fileName, uint32 *fileSize)
 				return locFileData;
 			}
 		}
-		return 0;
+		return nullptr;
 	}
 }
 
@@ -190,22 +190,23 @@ Common::SeekableReadStream *Resources::openFile(const Common::String &fileName)
 	// first try to find files outside of .pak
 	// some patched files have not been included in package.
 	if (Common::File::exists(fileName)) {
-		Common::File *file = new Common::File();
-		bool opened = file->open(fileName);
-		if (!opened) {
-			delete file;
-			return 0;
+		Common::File file;
+		if (file.open(fileName)) {
+			Common::SeekableReadStream *stream = file.readStream(file.size());
+			file.close();
+			return stream;
+		} else {
+			return nullptr;
 		}
-		return file;
 	} else {
 		for (uint32 i = 0; i < _pakFiles.size(); i++) {
-			Common::SeekableReadStream *stream = 0;
+			Common::SeekableReadStream *stream = nullptr;
 			stream = _pakFiles[i]->createReadStream(fileName);
 			if (stream)
 				return stream;
 		}
 
-		return 0;
+		return nullptr;
 	}
 }
 
@@ -224,7 +225,7 @@ Common::SeekableReadStream *PakFile::createReadStream(const Common::String &file
 	if (buffer)
 		return new Common::MemoryReadStream(buffer, fileSize, DisposeAfterUse::YES);
 	else
-		return 0;
+		return nullptr;
 }
 
 uint8 *PakFile::getFileData(const Common::String &fileName, uint32 *fileSize) {
diff --git a/engines/toon/subtitles.cpp b/engines/toon/subtitles.cpp
index e43a19386be..194434d9140 100644
--- a/engines/toon/subtitles.cpp
+++ b/engines/toon/subtitles.cpp
@@ -75,8 +75,8 @@ bool SubtitleRenderer::load(const Common::String &video) {
 	Common::String ext("tss");
 	subfile.replace(subfile.size() - ext.size(), ext.size(), ext);
 
-	Common::SeekableReadStream *file = _vm->resources()->openFile(subfile);
-	if (!file) {
+	Common::ScopedPtr<Common::SeekableReadStream> subsStream(_vm->resources()->openFile(subfile));
+	if (subsStream == nullptr) {
 		return false;
 	}
 
@@ -84,8 +84,8 @@ bool SubtitleRenderer::load(const Common::String &video) {
 	int lineNo = 0;
 
 	_tw.clear();
-	while (!file->eos() && !file->err()) {
-		line = file->readLine();
+	while (!subsStream->eos() && !subsStream->err()) {
+		line = subsStream->readLine();
 
 		lineNo++;
 		if (line.empty() || line[0] == '#') {




More information about the Scummvm-git-logs mailing list