From d1bfc1e50ce59034f2462b1430dfad397519ca16 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Tue, 10 Sep 2013 04:18:58 +0200 Subject: [PATCH] Properly free RWops associated with Font objects --- src/filesystem.cpp | 18 ++++++++++++++++-- src/filesystem.h | 3 ++- src/font.cpp | 2 +- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 5086b37..44b11a3 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -711,11 +711,21 @@ static int SDL_RWopsClose(SDL_RWops *ops) return (result != 0) ? 0 : -1; } +static int SDL_RWopsCloseFree(SDL_RWops *ops) +{ + int result = SDL_RWopsClose(ops); + + SDL_FreeRW(ops); + + return result; +} + const Uint32 SDL_RWOPS_PHYSFS = SDL_RWOPS_UNKNOWN+10; void FileSystem::openRead(SDL_RWops &ops, const char *filename, - FileType type) + FileType type, + bool freeOnClose) { PHYSFS_File *handle = openReadInt(filename, type); @@ -723,7 +733,11 @@ void FileSystem::openRead(SDL_RWops &ops, ops.seek = SDL_RWopsSeek; ops.read = SDL_RWopsRead; ops.write = SDL_RWopsWrite; - ops.close = SDL_RWopsClose; + + if (freeOnClose) + ops.close = SDL_RWopsCloseFree; + else + ops.close = SDL_RWopsClose; ops.type = SDL_RWOPS_PHYSFS; ops.hidden.unknown.data1 = handle; diff --git a/src/filesystem.h b/src/filesystem.h index 3e26228..1ecc493 100644 --- a/src/filesystem.h +++ b/src/filesystem.h @@ -71,7 +71,8 @@ public: void openRead(SDL_RWops &ops, const char *filename, - FileType type = Undefined); + FileType type = Undefined, + bool freeOnClose = false); bool exists(const char *filename, FileType type = Undefined); diff --git a/src/font.cpp b/src/font.cpp index 6808267..cd2e99d 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -107,7 +107,7 @@ _TTF_Font *FontPool::request(const char *filename, else { ops = SDL_AllocRW(); - gState->fileSystem().openRead(*ops, path.constData(), FileSystem::Font); + gState->fileSystem().openRead(*ops, path.constData(), FileSystem::Font, true); } // FIXME 0.9 is guesswork at this point