[Scummvm-git-logs] scummvm master -> bf406f66c0fe1985f90c46f8c08b092c8553016e
criezy
criezy at scummvm.org
Sat May 8 20:58:32 UTC 2021
This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .
Summary:
800c673b62 BACKENDS: SDL: Fix memory leak in supported graphics modes
bf406f66c0 BACKENDS: SDL: Do not allocate memory in SurfaceSdlGraphicsManager::getSupportedGraphicsModes
Commit: 800c673b62a24faa501939daed55e7563ebf2095
https://github.com/scummvm/scummvm/commit/800c673b62a24faa501939daed55e7563ebf2095
Author: Mathias Parnaudeau (mparnaudeau at optimum-software.fr)
Date: 2021-05-08T21:58:30+01:00
Commit Message:
BACKENDS: SDL: Fix memory leak in supported graphics modes
Supported graphics modes are grouped in an merged array in which
the first part contains modes from the non-OpenGL SDL manager,
obtained by a copy dynamically allocated.
When the array was rebuilt or aimed to be destroyed, it was only
cleared (removing elements) but not freeing duplicated fields
name and description.
An additional leak came from the temporary array (srcModes)
not freed.
Changed paths:
backends/platform/sdl/sdl.cpp
backends/platform/sdl/sdl.h
diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
index 6389d9c0f3..342fd9414e 100644
--- a/backends/platform/sdl/sdl.cpp
+++ b/backends/platform/sdl/sdl.cpp
@@ -78,7 +78,7 @@ struct LegacyGraphicsMode {
};
// Table for using old names for scalers in the configuration
-// to keep compatibiblity with old config files.
+// to keep compatibility with old config files.
static const LegacyGraphicsMode s_legacyGraphicsModes[] = {
{ "supereagle2x", "supereagle" },
{ "dotmatrix2x", "dotmatrix" },
@@ -112,6 +112,10 @@ OSystem_SDL::OSystem_SDL()
OSystem_SDL::~OSystem_SDL() {
SDL_ShowCursor(SDL_ENABLE);
+#ifdef USE_OPENGL
+ clearGraphicsModes();
+#endif
+
// Delete the various managers here. Note that the ModularBackend
// destructors would also take care of this for us. However, various
// of our managers must be deleted *before* we call SDL_Quit().
@@ -848,17 +852,19 @@ int OSystem_SDL::getGraphicsMode() const {
}
void OSystem_SDL::setupGraphicsModes() {
- _graphicsModes.clear();
+ clearGraphicsModes();
_graphicsModeIds.clear();
_defaultSDLMode = _defaultGLMode = -1;
// Count the number of graphics modes
+ const OSystem::GraphicsMode *srcModes;
const OSystem::GraphicsMode *srcMode;
int defaultMode;
GraphicsManager *manager = new SurfaceSdlGraphicsManager(_eventSource, _window);
- srcMode = manager->getSupportedGraphicsModes();
defaultMode = manager->getDefaultGraphicsMode();
+ srcModes = manager->getSupportedGraphicsModes();
+ srcMode = srcModes;
while (srcMode->name) {
if (defaultMode == srcMode->id) {
_defaultSDLMode = _graphicsModes.size();
@@ -866,6 +872,7 @@ void OSystem_SDL::setupGraphicsModes() {
_graphicsModes.push_back(*srcMode);
srcMode++;
}
+ delete[] srcModes;
delete manager;
assert(_defaultSDLMode != -1);
@@ -881,7 +888,6 @@ void OSystem_SDL::setupGraphicsModes() {
srcMode++;
}
delete manager;
- manager = nullptr;
assert(_defaultGLMode != -1);
// Set a null mode at the end
@@ -898,5 +904,19 @@ void OSystem_SDL::setupGraphicsModes() {
mode++;
}
}
-#endif
+void OSystem_SDL::clearGraphicsModes() {
+ if (!_graphicsModes.empty()) {
+ int i = 0;
+
+ OSystem::GraphicsMode *mode = _graphicsModes.begin();
+ while (mode->name && i < _firstGLMode) {
+ free(const_cast<char *>(mode->name));
+ free(const_cast<char *>(mode->description));
+ mode++;
+ i++;
+ }
+ _graphicsModes.clear();
+ }
+}
+#endif
diff --git a/backends/platform/sdl/sdl.h b/backends/platform/sdl/sdl.h
index 7eb7d8ba73..cd95be7b0f 100644
--- a/backends/platform/sdl/sdl.h
+++ b/backends/platform/sdl/sdl.h
@@ -139,7 +139,7 @@ protected:
#endif
/**
- * Initialze the SDL library.
+ * Initialize the SDL library.
*/
virtual void initSDL();
@@ -163,10 +163,15 @@ protected:
int _defaultGLMode;
/**
- * Creates the merged graphics modes list
+ * Create the merged graphics modes list.
*/
void setupGraphicsModes();
+ /**
+ * Clear the merged graphics modes list.
+ */
+ void clearGraphicsModes();
+
virtual const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
virtual int getDefaultGraphicsMode() const override;
virtual bool setGraphicsMode(int mode, uint flags) override;
Commit: bf406f66c0fe1985f90c46f8c08b092c8553016e
https://github.com/scummvm/scummvm/commit/bf406f66c0fe1985f90c46f8c08b092c8553016e
Author: Thierry Crozat (criezy at scummvm.org)
Date: 2021-05-08T21:58:30+01:00
Commit Message:
BACKENDS: SDL: Do not allocate memory in SurfaceSdlGraphicsManager::getSupportedGraphicsModes
Changed paths:
backends/graphics/surfacesdl/surfacesdl-graphics.cpp
backends/graphics/surfacesdl/surfacesdl-graphics.h
backends/platform/sdl/sdl.cpp
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
index 5e3aeb562e..b6d09a97fc 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.cpp
@@ -300,27 +300,8 @@ static void initGraphicsModes() {
s_supportedGraphicsModes->push_back(gm);
}
-const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::supportedGraphicsModes() const {
- if (!s_supportedGraphicsModes)
- initGraphicsModes();
-
- int size = s_supportedGraphicsModes->size();
- OSystem::GraphicsMode *modes = new OSystem::GraphicsMode[size];
- memcpy(modes, &(*s_supportedGraphicsModes)[0], size * sizeof(OSystem::GraphicsMode));
-
- // Do deep copy. Each can be freed independently of the other.
- OSystem::GraphicsMode *gm = modes;
- while (gm->name) {
- gm->name = scumm_strdup(gm->name);
- gm->description = scumm_strdup(gm->description);
- ++gm;
- }
-
- return modes;
-}
-
const OSystem::GraphicsMode *SurfaceSdlGraphicsManager::getSupportedGraphicsModes() const {
- return supportedGraphicsModes();
+ return s_supportedGraphicsModes->begin();
}
int SurfaceSdlGraphicsManager::getDefaultGraphicsMode() const {
diff --git a/backends/graphics/surfacesdl/surfacesdl-graphics.h b/backends/graphics/surfacesdl/surfacesdl-graphics.h
index 628a55740d..43083aa08f 100644
--- a/backends/graphics/surfacesdl/surfacesdl-graphics.h
+++ b/backends/graphics/surfacesdl/surfacesdl-graphics.h
@@ -65,7 +65,6 @@ public:
virtual void setFeatureState(OSystem::Feature f, bool enable) override;
virtual bool getFeatureState(OSystem::Feature f) const override;
- const OSystem::GraphicsMode *supportedGraphicsModes() const;
virtual const OSystem::GraphicsMode *getSupportedGraphicsModes() const override;
virtual int getDefaultGraphicsMode() const override;
virtual bool setGraphicsMode(int mode, uint flags = OSystem::kGfxModeNoFlags) override;
diff --git a/backends/platform/sdl/sdl.cpp b/backends/platform/sdl/sdl.cpp
index 342fd9414e..ca11dc12ec 100644
--- a/backends/platform/sdl/sdl.cpp
+++ b/backends/platform/sdl/sdl.cpp
@@ -857,22 +857,24 @@ void OSystem_SDL::setupGraphicsModes() {
_defaultSDLMode = _defaultGLMode = -1;
// Count the number of graphics modes
- const OSystem::GraphicsMode *srcModes;
const OSystem::GraphicsMode *srcMode;
int defaultMode;
GraphicsManager *manager = new SurfaceSdlGraphicsManager(_eventSource, _window);
defaultMode = manager->getDefaultGraphicsMode();
- srcModes = manager->getSupportedGraphicsModes();
- srcMode = srcModes;
+ srcMode = manager->getSupportedGraphicsModes();
while (srcMode->name) {
if (defaultMode == srcMode->id) {
_defaultSDLMode = _graphicsModes.size();
}
- _graphicsModes.push_back(*srcMode);
+ OSystem::GraphicsMode mode = *srcMode;
+ // Do deep copy as we are going to delete the GraphicsManager and this may free
+ // the memory used for its graphics modes.
+ mode.name = scumm_strdup(srcMode->name);
+ mode.description = scumm_strdup(srcMode->description);
+ _graphicsModes.push_back(mode);
srcMode++;
}
- delete[] srcModes;
delete manager;
assert(_defaultSDLMode != -1);
@@ -884,7 +886,12 @@ void OSystem_SDL::setupGraphicsModes() {
if (defaultMode == srcMode->id) {
_defaultGLMode = _graphicsModes.size();
}
- _graphicsModes.push_back(*srcMode);
+ OSystem::GraphicsMode mode = *srcMode;
+ // Do deep copy as we are going to delete the GraphicsManager and this may free
+ // the memory used for its graphics modes.
+ mode.name = scumm_strdup(srcMode->name);
+ mode.description = scumm_strdup(srcMode->description);
+ _graphicsModes.push_back(mode);
srcMode++;
}
delete manager;
@@ -907,14 +914,11 @@ void OSystem_SDL::setupGraphicsModes() {
void OSystem_SDL::clearGraphicsModes() {
if (!_graphicsModes.empty()) {
- int i = 0;
-
OSystem::GraphicsMode *mode = _graphicsModes.begin();
- while (mode->name && i < _firstGLMode) {
+ while (mode->name) {
free(const_cast<char *>(mode->name));
free(const_cast<char *>(mode->description));
mode++;
- i++;
}
_graphicsModes.clear();
}
More information about the Scummvm-git-logs
mailing list