summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/CMakeLists.txt2
-rw-r--r--src/Makefile.am2
-rw-r--r--src/debug.h14
-rw-r--r--src/localconsts.h3
-rw-r--r--src/main.cpp8
-rw-r--r--src/resources/atlasmanager.cpp3
-rw-r--r--src/resources/resourcemanager.cpp7
-rw-r--r--src/test/testlauncher.cpp5
-rw-r--r--src/utils/physfsrwops.cpp6
-rw-r--r--src/utils/physfsrwops.h2
-rw-r--r--src/utils/sdlcheckutils.cpp18
11 files changed, 53 insertions, 17 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 26bd600d2..507c52062 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -534,6 +534,8 @@ SET(SRCS
utils/paths.h
utils/perfomance.cpp
utils/perfomance.h
+ utils/physfscheckutils.cpp
+ utils/physfscheckutils.h
utils/physfsrwops.cpp
utils/physfsrwops.h
utils/physfstools.cpp
diff --git a/src/Makefile.am b/src/Makefile.am
index 8332f02cd..ce4d8ad64 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -547,6 +547,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \
utils/paths.h \
utils/perfomance.cpp \
utils/perfomance.h \
+ utils/physfscheckutils.cpp \
+ utils/physfscheckutils.h \
utils/physfsrwops.cpp \
utils/physfsrwops.h \
utils/physfstools.cpp \
diff --git a/src/debug.h b/src/debug.h
index 079cebeac..31b2b457b 100644
--- a/src/debug.h
+++ b/src/debug.h
@@ -57,7 +57,7 @@
#define MSDL_DisplayFormat(surface) \
FakeSDL_DisplayFormat(surface, __FILE__, __LINE__)
-#else
+#else // ENABLE_SDL_DEBUG
#define MIMG_LoadPNG_RW(src) IMG_LoadPNG_RW(src)
#define MSDL_FreeSurface(surface) SDL_FreeSurface(surface)
@@ -72,3 +72,15 @@
#define MSDL_DisplayFormat(surface) SDL_DisplayFormat(surface)
#endif // ENABLE_SDL_DEBUG
+
+
+#ifdef DEBUG_PHYSFS
+
+#define MPHYSFSRWOPS_openRead(name) \
+ FakePHYSFSRWOPS_openRead(name, __FILE__, __LINE__)
+
+#else // DEBUG_PHYSFS
+
+#define MPHYSFSRWOPS_openRead(name) PHYSFSRWOPS_openRead(name)
+
+#endif // DEBUG_PHYSFS
diff --git a/src/localconsts.h b/src/localconsts.h
index 205920746..4187cefd2 100644
--- a/src/localconsts.h
+++ b/src/localconsts.h
@@ -102,6 +102,9 @@
// debug SDL surfaces
// #define DEBUG_SDL_SURFACES 1
+// debug RWops usage
+// #define DEBUG_PHYSFS 1
+
// use file access fuzzer
// #define USE_FUZZER 1
diff --git a/src/main.cpp b/src/main.cpp
index 236847ad1..bec85f4c8 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -34,9 +34,10 @@
#include "utils/mkdir.h"
#endif
#include "utils/paths.h"
+#include "utils/physfscheckutils.h"
+#include "utils/physfstools.h"
#include "utils/physfsrwops.h"
#include "utils/process.h"
-#include "utils/physfstools.h"
#include "utils/xml.h"
#ifdef UNITTESTS
@@ -310,7 +311,12 @@ int main(int argc, char *argv[])
}
delete client;
client = nullptr;
+#ifdef DUMP_LEAKED_RESOURCES
reportRWops();
+#endif
+#ifdef DEBUG_PHYSFS
+ reportPhysfsLeaks();
+#endif
return ret;
}
diff --git a/src/resources/atlasmanager.cpp b/src/resources/atlasmanager.cpp
index b0ac91540..b60ee0a50 100644
--- a/src/resources/atlasmanager.cpp
+++ b/src/resources/atlasmanager.cpp
@@ -31,6 +31,7 @@
#include "render/surfacegraphics.h"
#include "utils/mathutils.h"
+#include "utils/physfscheckutils.h"
#include "utils/physfsrwops.h"
#include "utils/sdlcheckutils.h"
@@ -116,7 +117,7 @@ void AtlasManager::loadImages(const StringVect &files,
path = path.substr(0, p);
}
- SDL_RWops *const rw = PHYSFSRWOPS_openRead(path.c_str());
+ SDL_RWops *const rw = MPHYSFSRWOPS_openRead(path.c_str());
if (rw)
{
Image *const image = d ? surfaceImageHelper->load(rw, *d)
diff --git a/src/resources/resourcemanager.cpp b/src/resources/resourcemanager.cpp
index 3d13e6e00..bd422ca0e 100644
--- a/src/resources/resourcemanager.cpp
+++ b/src/resources/resourcemanager.cpp
@@ -41,6 +41,7 @@
#include "resources/spritedef.h"
#include "utils/mkdir.h"
+#include "utils/physfscheckutils.h"
#include "utils/physfsrwops.h"
#include "utils/sdlcheckutils.h"
@@ -567,7 +568,7 @@ struct ResourceLoader
return nullptr;
const ResourceLoader *const
rl = static_cast<const ResourceLoader *const>(v);
- SDL_RWops *const rw = PHYSFSRWOPS_openRead(rl->path.c_str());
+ SDL_RWops *const rw = MPHYSFSRWOPS_openRead(rl->path.c_str());
if (!rw)
return nullptr;
Resource *const res = rl->fun(rw);
@@ -613,7 +614,7 @@ struct DyedImageLoader
d = new Dye(path.substr(p + 1));
path = path.substr(0, p);
}
- SDL_RWops *const rw = PHYSFSRWOPS_openRead(path.c_str());
+ SDL_RWops *const rw = MPHYSFSRWOPS_openRead(path.c_str());
if (!rw)
{
delete d;
@@ -1044,7 +1045,7 @@ void ResourceManager::saveTextFile(std::string path, const std::string &name,
SDL_Surface *ResourceManager::loadSDLSurface(const std::string &filename) const
{
- if (SDL_RWops *const rw = PHYSFSRWOPS_openRead(filename.c_str()))
+ if (SDL_RWops *const rw = MPHYSFSRWOPS_openRead(filename.c_str()))
{
if (!IMG_isPNG(rw))
{
diff --git a/src/test/testlauncher.cpp b/src/test/testlauncher.cpp
index 29b66e20e..82cfd4d6b 100644
--- a/src/test/testlauncher.cpp
+++ b/src/test/testlauncher.cpp
@@ -33,6 +33,7 @@
#include "utils/gettext.h"
#include "utils/mkdir.h"
+#include "utils/physfscheckutils.h"
#include "utils/physfsrwops.h"
#include "resources/dye.h"
@@ -236,7 +237,7 @@ int TestLauncher::testInternal()
int TestLauncher::testDye()
{
- SDL_RWops *rw = PHYSFSRWOPS_openRead(
+ SDL_RWops *rw = MPHYSFSRWOPS_openRead(
"graphics/sprites/arrow_up.png");
Dye *d = nullptr;
@@ -259,7 +260,7 @@ int TestLauncher::testDye()
client->getTempDirectory() + "/testimage2.png");
}
- rw = PHYSFSRWOPS_openRead(
+ rw = MPHYSFSRWOPS_openRead(
"graphics/sprites/arrow_up.png");
d = new Dye("S:#0000ff,00ff00,5c5cff,ff0000");
image = d ? surfaceImageHelper->load(rw, *d)
diff --git a/src/utils/physfsrwops.cpp b/src/utils/physfsrwops.cpp
index 022e8160f..aa5caf7c7 100644
--- a/src/utils/physfsrwops.cpp
+++ b/src/utils/physfsrwops.cpp
@@ -27,6 +27,7 @@
#include "logger.h"
#include "utils/fuzzer.h"
+#include "utils/physfscheckutils.h"
#include <stdio.h>
@@ -161,6 +162,9 @@ static int physfsrwops_close(SDL_RWops *const rw)
logger->log("closing already closed RWops");
openedRWops --;
#endif
+#ifdef DEBUG_PHYSFS
+ FakePhysFSClose(rw);
+#endif
return 0;
} /* physfsrwops_close */
@@ -256,10 +260,12 @@ SDL_RWops *PHYSFSRWOPS_openAppend(const char *const fname)
return create_rwops(PhysFs::openAppend(fname));
} /* PHYSFSRWOPS_openAppend */
+#ifdef DUMP_LEAKED_RESOURCES
void reportRWops()
{
if (openedRWops)
logger->log("leaking RWops: %d", openedRWops);
}
+#endif
/* end of physfsrwops.c ... */
diff --git a/src/utils/physfsrwops.h b/src/utils/physfsrwops.h
index 45ee21cc4..7e859a234 100644
--- a/src/utils/physfsrwops.h
+++ b/src/utils/physfsrwops.h
@@ -79,6 +79,8 @@ SDL_RWops *PHYSFSRWOPS_openAppend(const char *const fname) A_WARN_UNUSED;
*/
SDL_RWops *PHYSFSRWOPS_makeRWops(PHYSFS_file *const handle) A_WARN_UNUSED;
+#ifdef DUMP_LEAKED_RESOURCES
void reportRWops();
+#endif
#endif // UTILS_PHYSFSRWOPS_H
diff --git a/src/utils/sdlcheckutils.cpp b/src/utils/sdlcheckutils.cpp
index 50adedb28..3810672d9 100644
--- a/src/utils/sdlcheckutils.cpp
+++ b/src/utils/sdlcheckutils.cpp
@@ -35,10 +35,10 @@
#define DEBUG_SURFACE_ALLOCATION 1
-struct SurfaceObject
+struct MemoryObject
{
- SurfaceObject(const std::string &name, const char *const file,
- const unsigned int line) :
+ MemoryObject(const std::string &name, const char *const file,
+ const unsigned int line) :
mName(name),
mAddFile(strprintf("%s:%u", file, line)),
mRemoveFile(),
@@ -52,7 +52,7 @@ struct SurfaceObject
int mCnt;
};
-std::map<SDL_Surface*, SurfaceObject*> mSurfaces;
+std::map<void*, MemoryObject*> mSurfaces;
static SDL_Surface *addSurface(const char *const name,
SDL_Surface *const surface,
@@ -63,11 +63,11 @@ static SDL_Surface *addSurface(const char *const name,
logger->log("add surface: %s %s:%u %p", name,
file, line, static_cast<void*>(surface));
#endif
- std::map<SDL_Surface*, SurfaceObject*>::iterator
+ std::map<void*, MemoryObject*>::iterator
it = mSurfaces.find(surface);
if (it != mSurfaces.end())
{
- SurfaceObject *const obj = (*it).second;
+ MemoryObject *const obj = (*it).second;
if (obj)
{ // found some time ago created surface
#ifdef DEBUG_SURFACE_ALLOCATION
@@ -80,7 +80,7 @@ static SDL_Surface *addSurface(const char *const name,
}
else
{ // creating surface object
- mSurfaces[surface] = new SurfaceObject(name, file, line);
+ mSurfaces[surface] = new MemoryObject(name, file, line);
}
return surface;
}
@@ -93,7 +93,7 @@ static void deleteSurface(const char *const name A_UNUSED,
#ifdef DEBUG_SURFACE_ALLOCATION
logger->log("delete surface: %s %s:%u %p", name, file, line, surface);
#endif
- std::map<SDL_Surface*, SurfaceObject*>::iterator
+ std::map<void*, MemoryObject*>::iterator
it = mSurfaces.find(surface);
if (it == mSurfaces.end())
{
@@ -102,7 +102,7 @@ static void deleteSurface(const char *const name A_UNUSED,
}
else
{
- SurfaceObject *const obj = (*it).second;
+ MemoryObject *const obj = (*it).second;
if (obj)
{
const int cnt = obj->mCnt;