diff options
author | Andrei Karas <akaras@inbox.ru> | 2017-04-07 02:48:12 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2017-04-08 16:46:28 +0300 |
commit | 27a2dd4fce2d529cd1d96aa5ddce1e74b4f54a2f (patch) | |
tree | c2c38e1b4f308ca885dc3c3581560cfbc6833a8e /src | |
parent | d4e28aa97cbce34a0053d3c02cf070ef9065439f (diff) | |
download | manaplus-27a2dd4fce2d529cd1d96aa5ddce1e74b4f54a2f.tar.gz manaplus-27a2dd4fce2d529cd1d96aa5ddce1e74b4f54a2f.tar.bz2 manaplus-27a2dd4fce2d529cd1d96aa5ddce1e74b4f54a2f.tar.xz manaplus-27a2dd4fce2d529cd1d96aa5ddce1e74b4f54a2f.zip |
Move resourcemanager from class to namespace.
Diffstat (limited to 'src')
45 files changed, 723 insertions, 488 deletions
diff --git a/src/actions/actions.cpp b/src/actions/actions.cpp index b4fadf62d..c6d48a1d9 100644 --- a/src/actions/actions.cpp +++ b/src/actions/actions.cpp @@ -1038,7 +1038,7 @@ impHandler0(who) impHandler0(cleanGraphics) { - resourceManager->clearCache(); + ResourceManager::clearCache(); if (debugChatTab) { @@ -1274,20 +1274,20 @@ impHandler(dump) if (!event.args.empty()) { - ResourceManager::Resources *res = resourceManager->getResources(); + ResourceManager::Resources *res = ResourceManager::getResources(); // TRANSLATORS: dump command showRes(_("Resource images:"), res); - res = resourceManager->getOrphanedResources(); + res = ResourceManager::getOrphanedResources(); // TRANSLATORS: dump command showRes(_("Resource orphaned images:"), res); } else { - ResourceManager::Resources *res = resourceManager->getResources(); + ResourceManager::Resources *res = ResourceManager::getResources(); // TRANSLATORS: dump command debugChatTab->chatLog(_("Resource images:") + toString(res->size()), ChatMsgType::BY_SERVER); - res = resourceManager->getOrphanedResources(); + res = ResourceManager::getOrphanedResources(); // TRANSLATORS: dump command debugChatTab->chatLog(_("Resource orphaned images:") + toString(res->size()), diff --git a/src/client.cpp b/src/client.cpp index f1ede82a2..2868b675d 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -334,7 +334,6 @@ void Client::gameInit() ConfigManager::storeSafeParameters(); #endif // ANDROID - ResourceManager::init(); if (!VirtFs::setWriteDir(settings.localDataDir)) { logger->error(strprintf("%s couldn't be set as home directory! " @@ -1039,7 +1038,7 @@ int Client::gameExec() delete2(mGame); assertListener = new AssertListener; Game::clearInstance(); - resourceManager->cleanOrphans(); + ResourceManager::cleanOrphans(); Party::clearParties(); Guild::clearGuilds(); NpcDialog::clearDialogs(); @@ -1111,7 +1110,7 @@ int Client::gameExec() "local/")); } - resourceManager->clearCache(); + ResourceManager::clearCache(); loginData.clearUpdateHost(); serverVersion = 0; diff --git a/src/dyetool/client.cpp b/src/dyetool/client.cpp index cf3fceed6..0815272d4 100644 --- a/src/dyetool/client.cpp +++ b/src/dyetool/client.cpp @@ -189,7 +189,6 @@ void Client::gameInit() ConfigManager::storeSafeParameters(); #endif // ANDROID - ResourceManager::init(); if (!VirtFs::setWriteDir(settings.localDataDir)) { logger->error(strprintf("%s couldn't be set as home directory! " diff --git a/src/dyetool/dyemain.cpp b/src/dyetool/dyemain.cpp index 3b6fafb3b..9ee1bd0a9 100644 --- a/src/dyetool/dyemain.cpp +++ b/src/dyetool/dyemain.cpp @@ -87,7 +87,6 @@ int main(int argc, char **argv) imageHelper = new SDLImageHelper; #endif // USE_SDL2 - ResourceManager::init(); VirtFs::setWriteDir("."); VirtFs::mountDir(".", Append_false); VirtFs::mountDir("/", Append_false); diff --git a/src/fs/files_unittest.cc b/src/fs/files_unittest.cc index bb9822343..bc4c2dbf6 100644 --- a/src/fs/files_unittest.cc +++ b/src/fs/files_unittest.cc @@ -35,7 +35,6 @@ TEST_CASE("Files renameFile") { logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -74,7 +73,6 @@ TEST_CASE("Files renameFile") TEST_CASE("Files existsLocal") { logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); REQUIRE(Files::existsLocal(VirtFs::getPath("help/about.txt")) == true); @@ -90,7 +88,6 @@ TEST_CASE("Files existsLocal") TEST_CASE("Files loadTextFileString") { logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); REQUIRE(VirtFs::loadTextFileString("test/simplefile.txt") == @@ -105,7 +102,6 @@ TEST_CASE("Files loadTextFileString") TEST_CASE("Files loadTextFile") { logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -124,7 +120,6 @@ TEST_CASE("Files loadTextFile") TEST_CASE("Files saveTextFile") { logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -150,7 +145,6 @@ TEST_CASE("Files saveTextFile") TEST_CASE("Files getFilesInDir") { logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); diff --git a/src/game.cpp b/src/game.cpp index a00c6cfea..c8d51aff4 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1026,7 +1026,7 @@ void Game::changeMap(const std::string &mapPath) BLOCK_START("Game::changeMap") resetAdjustLevel(); - resourceManager->cleanProtected(); + ResourceManager::cleanProtected(); if (popupManager) { diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 06afeccc6..227b0c463 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -300,7 +300,7 @@ Gui::~Gui() void Gui::logic() { BLOCK_START("Gui::logic") - resourceManager->clearScheduled(); + ResourceManager::clearScheduled(); if (!mTop) { @@ -359,8 +359,7 @@ void Gui::slowLogic() if (time > mTime10 || mTime10 - time > 10) { mTime10 = time + 10; - if (resourceManager) - resourceManager->cleanOrphans(); + ResourceManager::cleanOrphans(); } } diff --git a/src/gui/widgets/browserbox_unittest.cc b/src/gui/widgets/browserbox_unittest.cc index 6064dfd72..e86aa6b0e 100644 --- a/src/gui/widgets/browserbox_unittest.cc +++ b/src/gui/widgets/browserbox_unittest.cc @@ -47,11 +47,19 @@ extern const char *dirSeparator; +TEST_CASE("BrowserBox leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("BrowserBox tests", "browserbox") { client = new Client; logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -189,3 +197,12 @@ TEST_CASE("BrowserBox tests", "browserbox") delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("BrowserBox leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/gui/widgets/desktop.cpp b/src/gui/widgets/desktop.cpp index 4aeea03b5..83d5bd58e 100644 --- a/src/gui/widgets/desktop.cpp +++ b/src/gui/widgets/desktop.cpp @@ -182,7 +182,7 @@ void Desktop::setBestFittingWallpaper() { if (mWallpaper) { - resourceManager->decRefDelete(mWallpaper); + ResourceManager::decRefDelete(mWallpaper); mWallpaper = nullptr; } @@ -200,7 +200,7 @@ void Desktop::setBestFittingWallpaper() if (newRsclWlPpr) { - resourceManager->decRefDelete(nWallPaper); + ResourceManager::decRefDelete(nWallPaper); // We replace the resource in the resource manager mWallpaper = newRsclWlPpr; } diff --git a/src/gui/windowmanager_unittest.cc b/src/gui/windowmanager_unittest.cc index 49ea9d97c..b19844ac9 100644 --- a/src/gui/windowmanager_unittest.cc +++ b/src/gui/windowmanager_unittest.cc @@ -130,6 +130,15 @@ extern QuitDialog *quitDialog; +TEST_CASE("windows leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("Windows tests", "windowmanager") { setEnv("SDL_VIDEODRIVER", "dummy"); @@ -139,8 +148,7 @@ TEST_CASE("Windows tests", "windowmanager") SDL_Init(SDL_INIT_VIDEO); logger = new Logger(); ResourceManager::deleteInstance(); - ResourceManager::init(); - resourceManager->cleanOrphans(true); + ResourceManager::cleanOrphans(true); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); VirtFs::mountDirSilent("data/test", Append_false); @@ -723,8 +731,7 @@ TEST_CASE("WindowManager", "create windows") SDL_Init(SDL_INIT_VIDEO); logger = new Logger(); ResourceManager::deleteInstance(); - ResourceManager::init(); - resourceManager->cleanOrphans(true); + ResourceManager::cleanOrphans(true); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); VirtFs::mountDirSilent("data/test", Append_false); @@ -869,3 +876,12 @@ TEST_CASE("WindowManager", "create windows") // VirtFs::deinit(); } + +TEST_CASE("windows leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/integrity_unittest.cc b/src/integrity_unittest.cc index 34312b7b3..41a57f41d 100644 --- a/src/integrity_unittest.cc +++ b/src/integrity_unittest.cc @@ -30,6 +30,8 @@ #include "fs/virtfs/virtfs.h" #include "fs/virtfs/virtfsrwops.h" +#include "gui/gui.h" + #include "input/inputactionmap.h" #include "resources/image/image.h" @@ -93,6 +95,15 @@ static bool compareBuffers(const unsigned char *const buf2) return isCorrect; } +TEST_CASE("integrity leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("integrity tests", "integrity") { setEnv("SDL_VIDEODRIVER", "dummy"); @@ -101,7 +112,6 @@ TEST_CASE("integrity tests", "integrity") XML::initXML(); SDL_Init(SDL_INIT_VIDEO); logger = new Logger(); - ResourceManager::init(); std::string name("data/test/test.zip"); std::string prefix; if (Files::existsLocal(name) == false) @@ -326,7 +336,7 @@ TEST_CASE("integrity tests", "integrity") image->decRef(); } - resourceManager->cleanOrphans(true); + ResourceManager::cleanOrphans(true); delete client; client = nullptr; @@ -336,3 +346,12 @@ TEST_CASE("integrity tests", "integrity") delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("integrity leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/resources/atlas/atlasmanager.cpp b/src/resources/atlas/atlasmanager.cpp index b9107a757..91749e597 100644 --- a/src/resources/atlas/atlasmanager.cpp +++ b/src/resources/atlas/atlasmanager.cpp @@ -132,12 +132,12 @@ void AtlasManager::loadImages(const StringVect &files, const std::string str = *it; // check is image with same name already in cache // and if yes, move it to deleted set - Resource *const res = resourceManager->getTempResource(str); + Resource *const res = ResourceManager::getTempResource(str); if (res) { // increase counter because in moveToDeleted it will be decreased. res->incRef(); - resourceManager->moveToDeleted(res); + ResourceManager::moveToDeleted(res); } std::string path = str; @@ -181,12 +181,12 @@ void AtlasManager::loadEmptyImages(const StringVect &files, const std::string str = *it; // check is image with same name already in cache // and if yes, move it to deleted set - Resource *const res = resourceManager->getTempResource(str); + Resource *const res = ResourceManager::getTempResource(str); if (res) { // increase counter because in moveToDeleted it will be decreased. res->incRef(); - resourceManager->moveToDeleted(res); + ResourceManager::moveToDeleted(res); } Image *const image = new Image(0, @@ -395,14 +395,14 @@ void AtlasManager::injectToResources(const AtlasResource *const resource) { Image *const image = atlas->atlasImage; if (image) - resourceManager->addResource(atlas->name, image); + ResourceManager::addResource(atlas->name, image); FOR_EACH (std::vector<AtlasItem*>::iterator, it2, atlas->items) { AtlasItem *const item = *it2; if (!item) continue; // add each atlas sub image to resources - resourceManager->addResource(item->name, item->image); + ResourceManager::addResource(item->name, item->image); } } } @@ -422,7 +422,7 @@ void AtlasManager::moveToDeleted(AtlasResource *const resource) if (image) { // move each atlas image to deleted - resourceManager->moveToDeleted(image); + ResourceManager::moveToDeleted(image); } FOR_EACH (std::vector<AtlasItem*>::iterator, it2, atlas->items) { @@ -433,7 +433,7 @@ void AtlasManager::moveToDeleted(AtlasResource *const resource) if (image2) { // move each atlas sub image to deleted - resourceManager->moveToDeleted(image2); + ResourceManager::moveToDeleted(image2); } } } diff --git a/src/resources/atlas/atlasresource.cpp b/src/resources/atlas/atlasresource.cpp index 5f2a3d3c7..06ae62542 100644 --- a/src/resources/atlas/atlasresource.cpp +++ b/src/resources/atlas/atlasresource.cpp @@ -53,7 +53,7 @@ AtlasResource::~AtlasResource() delete atlas; } } - resourceManager->clearDeleted(false); + ResourceManager::clearDeleted(false); } void AtlasResource::incRef() diff --git a/src/resources/dye/dye_unittest.cc b/src/resources/dye/dye_unittest.cc index 4323a28f6..f31097cbe 100644 --- a/src/resources/dye/dye_unittest.cc +++ b/src/resources/dye/dye_unittest.cc @@ -28,6 +28,8 @@ #include "fs/virtfs/virtfs.h" +#include "gui/gui.h" + #include "resources/sdlimagehelper.h" #ifdef USE_SDL2 #include "resources/surfaceimagehelper.h" @@ -53,6 +55,15 @@ #ifdef USE_OPENGL +TEST_CASE("Dye leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("Dye replaceSOGLColor 1 1") { DyePalette palette("#00ff00,000011", 6); @@ -2357,7 +2368,6 @@ TEST_CASE("Dye real dye") client = new Client; SDL_Init(SDL_INIT_VIDEO); logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); VirtFs::mountDirSilent("data/test", Append_false); @@ -2400,3 +2410,12 @@ TEST_CASE("Dye real dye") delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("Dye leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/resources/dye/dyepalette_unittest.cc b/src/resources/dye/dyepalette_unittest.cc index 14e5e5d49..280a74ead 100644 --- a/src/resources/dye/dyepalette_unittest.cc +++ b/src/resources/dye/dyepalette_unittest.cc @@ -52,6 +52,15 @@ #include "debug.h" +TEST_CASE("DyePalette leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("DyePalette tests") { setEnv("SDL_VIDEODRIVER", "dummy"); @@ -60,7 +69,6 @@ TEST_CASE("DyePalette tests") XML::initXML(); SDL_Init(SDL_INIT_VIDEO); logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); VirtFs::mountDirSilent("data/test", Append_false); @@ -399,3 +407,12 @@ TEST_CASE("DyePalette tests") delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("DyePalette leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/resources/image/image.cpp b/src/resources/image/image.cpp index 390bf6cf1..f295be9cb 100644 --- a/src/resources/image/image.cpp +++ b/src/resources/image/image.cpp @@ -215,7 +215,7 @@ void Image::SDLCleanCache() i != i_end; ++i) { if (mSDLSurface != i->second) - resourceManager->scheduleDelete(i->second); + ResourceManager::scheduleDelete(i->second); i->second = nullptr; } mAlphaCache.clear(); diff --git a/src/resources/loaders/atlasloader.cpp b/src/resources/loaders/atlasloader.cpp index c5f6691a3..1faa98b44 100644 --- a/src/resources/loaders/atlasloader.cpp +++ b/src/resources/loaders/atlasloader.cpp @@ -56,7 +56,7 @@ Resource *Loader::getAtlas(const std::string &name, const StringVect &files) { AtlasLoader rl = { name, &files }; - return resourceManager->get("atlas_" + name, AtlasLoader::load, &rl); + return ResourceManager::get("atlas_" + name, AtlasLoader::load, &rl); } #endif // USE_OPENGL diff --git a/src/resources/loaders/emptyatlasloader.cpp b/src/resources/loaders/emptyatlasloader.cpp index 81d99cd51..6fe2f1463 100644 --- a/src/resources/loaders/emptyatlasloader.cpp +++ b/src/resources/loaders/emptyatlasloader.cpp @@ -57,7 +57,7 @@ Resource *Loader::getEmptyAtlas(const std::string &name, const StringVect &files) { EmptyAtlasLoader rl = { name, &files }; - return resourceManager->get("atlas_" + name, EmptyAtlasLoader::load, &rl); + return ResourceManager::get("atlas_" + name, EmptyAtlasLoader::load, &rl); } #endif // USE_OPENGL diff --git a/src/resources/loaders/imageloader.cpp b/src/resources/loaders/imageloader.cpp index 2bfba2706..5ae883886 100644 --- a/src/resources/loaders/imageloader.cpp +++ b/src/resources/loaders/imageloader.cpp @@ -85,6 +85,6 @@ namespace Image *Loader::getImage(const std::string &idPath) { DyedImageLoader rl = { idPath }; - return static_cast<Image*>(resourceManager->get(idPath, + return static_cast<Image*>(ResourceManager::get(idPath, DyedImageLoader::load, &rl)); } diff --git a/src/resources/loaders/imagesetloader.cpp b/src/resources/loaders/imagesetloader.cpp index 6ec02c699..468b97120 100644 --- a/src/resources/loaders/imagesetloader.cpp +++ b/src/resources/loaders/imagesetloader.cpp @@ -74,6 +74,6 @@ ImageSet *Loader::getImageSet(const std::string &imagePath, "x").append(toString( h)).append( "]"); - return static_cast<ImageSet*>(resourceManager->get(str, + return static_cast<ImageSet*>(ResourceManager::get(str, ImageSetLoader::load, &rl)); } diff --git a/src/resources/loaders/musicloader.cpp b/src/resources/loaders/musicloader.cpp index f23a9fc70..0d482b8a6 100644 --- a/src/resources/loaders/musicloader.cpp +++ b/src/resources/loaders/musicloader.cpp @@ -78,6 +78,6 @@ namespace SDLMusic *Loader::getMusic(const std::string &idPath) { ResourceLoader rl = { idPath }; - return static_cast<SDLMusic*>(resourceManager->get( + return static_cast<SDLMusic*>(ResourceManager::get( idPath, ResourceLoader::load, &rl)); } diff --git a/src/resources/loaders/rescaledloader.cpp b/src/resources/loaders/rescaledloader.cpp index c4c315227..e4a9112f1 100644 --- a/src/resources/loaders/rescaledloader.cpp +++ b/src/resources/loaders/rescaledloader.cpp @@ -72,6 +72,6 @@ Image *Loader::getRescaled(const Image *const image, "_rescaled%dx%d", width, height); const RescaledLoader rl = { image, width, height }; Image *const img = static_cast<Image *const>( - resourceManager->get(idPath, RescaledLoader::load, &rl)); + ResourceManager::get(idPath, RescaledLoader::load, &rl)); return img; } diff --git a/src/resources/loaders/shaderloader.cpp b/src/resources/loaders/shaderloader.cpp index f06c9d219..d2ffba348 100644 --- a/src/resources/loaders/shaderloader.cpp +++ b/src/resources/loaders/shaderloader.cpp @@ -61,7 +61,7 @@ Resource *Loader::getShader(const unsigned int type, const std::string &name) { ShaderLoader rl = { name, type }; - return resourceManager->get("shader_" + name, ShaderLoader::load, &rl); + return ResourceManager::get("shader_" + name, ShaderLoader::load, &rl); } #endif // USE_OPENGL diff --git a/src/resources/loaders/shaderprogramloader.cpp b/src/resources/loaders/shaderprogramloader.cpp index d48e1aa9f..763536a6c 100644 --- a/src/resources/loaders/shaderprogramloader.cpp +++ b/src/resources/loaders/shaderprogramloader.cpp @@ -66,7 +66,7 @@ Resource *Loader::getShaderProgram(const std::string &vertex, const bool isNewShader) { ShaderProgramLoader rl = { vertex, fragment, isNewShader }; - return resourceManager->get("program_" + vertex + " + " + fragment, + return ResourceManager::get("program_" + vertex + " + " + fragment, ShaderProgramLoader::load, &rl); } diff --git a/src/resources/loaders/soundloader.cpp b/src/resources/loaders/soundloader.cpp index be71a5ddb..bff769755 100644 --- a/src/resources/loaders/soundloader.cpp +++ b/src/resources/loaders/soundloader.cpp @@ -73,6 +73,6 @@ namespace SoundEffect *Loader::getSoundEffect(const std::string &idPath) { ResourceLoader rl = { idPath }; - return static_cast<SoundEffect*>(resourceManager->get( + return static_cast<SoundEffect*>(ResourceManager::get( idPath, ResourceLoader::load, &rl)); } diff --git a/src/resources/loaders/spritedefloader.cpp b/src/resources/loaders/spritedefloader.cpp index c140c8f73..bdc0c40f0 100644 --- a/src/resources/loaders/spritedefloader.cpp +++ b/src/resources/loaders/spritedefloader.cpp @@ -63,6 +63,6 @@ SpriteDef *Loader::getSprite(const std::string &path, "[").append(toString( variant).append( "]")); - return static_cast<SpriteDef*>(resourceManager->get(str, + return static_cast<SpriteDef*>(ResourceManager::get(str, SpriteDefLoader::load, &rl)); } diff --git a/src/resources/loaders/subimageloader.cpp b/src/resources/loaders/subimageloader.cpp index f2f7ad7de..804e957a5 100644 --- a/src/resources/loaders/subimageloader.cpp +++ b/src/resources/loaders/subimageloader.cpp @@ -85,6 +85,6 @@ Image *Loader::getSubImage(Image *const parent, "x").append( toString(height)).append( "]"); - return static_cast<Image*>(resourceManager->get(str, + return static_cast<Image*>(ResourceManager::get(str, SubImageLoader::load, &rl)); } diff --git a/src/resources/loaders/subimagesetloader.cpp b/src/resources/loaders/subimagesetloader.cpp index c8d0fba74..6ca43cb32 100644 --- a/src/resources/loaders/subimagesetloader.cpp +++ b/src/resources/loaders/subimagesetloader.cpp @@ -73,6 +73,6 @@ ImageSet *Loader::getSubImageSet(Image *const parent, "x").append(toString( height)).append( "]"); - return static_cast<ImageSet*>(resourceManager->get(str, + return static_cast<ImageSet*>(ResourceManager::get(str, SubImageSetLoader::load, &rl)); } diff --git a/src/resources/loaders/walklayerloader.cpp b/src/resources/loaders/walklayerloader.cpp index 71f94539a..99d7a251f 100644 --- a/src/resources/loaders/walklayerloader.cpp +++ b/src/resources/loaders/walklayerloader.cpp @@ -58,7 +58,7 @@ WalkLayer *Loader::getWalkLayer(const std::string &name, Map *const map) { WalkLayerLoader rl = {name, map}; - return static_cast<WalkLayer*>(resourceManager->get("map_" + name, + return static_cast<WalkLayer*>(ResourceManager::get("map_" + name, WalkLayerLoader::load, &rl)); } #else // DYECMD diff --git a/src/resources/loaders/xmlloader.cpp b/src/resources/loaders/xmlloader.cpp index ac005bc06..74dd46ef9 100644 --- a/src/resources/loaders/xmlloader.cpp +++ b/src/resources/loaders/xmlloader.cpp @@ -58,7 +58,7 @@ XML::Document *Loader::getXml(const std::string &idPath, const SkipError skipError) { ResourceLoader rl = { idPath, useResman, skipError }; - return static_cast<XML::Document*>(resourceManager->get( + return static_cast<XML::Document*>(ResourceManager::get( idPath, ResourceLoader::load, &rl)); } diff --git a/src/resources/map/maplayer_unittest.cc b/src/resources/map/maplayer_unittest.cc index 945c52b0e..e8066029c 100644 --- a/src/resources/map/maplayer_unittest.cc +++ b/src/resources/map/maplayer_unittest.cc @@ -1649,7 +1649,6 @@ TEST_CASE("MapLayer draw") TEST_CASE("MapLayer drawSpecialLayer (specialLayer)") { logger = new Logger; - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -2002,7 +2001,6 @@ TEST_CASE("MapLayer drawSpecialLayer (specialLayer)") TEST_CASE("MapLayer drawSpecialLayer (tempLayer)") { logger = new Logger; - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -2361,7 +2359,6 @@ TEST_CASE("MapLayer drawSpecialLayer (tempLayer)") TEST_CASE("MapLayer drawFringe") { logger = new Logger; - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); diff --git a/src/resources/map/speciallayer_unittest.cc b/src/resources/map/speciallayer_unittest.cc index 93270d7e5..d43daaac8 100644 --- a/src/resources/map/speciallayer_unittest.cc +++ b/src/resources/map/speciallayer_unittest.cc @@ -30,6 +30,8 @@ #include "fs/virtfs/virtfs.h" +#include "gui/gui.h" + #include "utils/delete2.h" #include "resources/sdlimagehelper.h" @@ -40,11 +42,19 @@ #include "debug.h" +TEST_CASE("SpecialLayer leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("SpecialLayer updateCache") { logger = new Logger; client = new Client; - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -260,9 +270,18 @@ TEST_CASE("SpecialLayer updateCache") } delete layer; - resourceManager->cleanOrphans(); + ResourceManager::cleanOrphans(); delete2(client); VirtFs::unmountDirSilent("data"); VirtFs::unmountDirSilent("../data"); delete2(logger); } + +TEST_CASE("SpecialLayer leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/resources/memorymanager.cpp b/src/resources/memorymanager.cpp index 01e67eb1b..55d157c5f 100644 --- a/src/resources/memorymanager.cpp +++ b/src/resources/memorymanager.cpp @@ -80,14 +80,12 @@ void MemoryManager::printAllMemory(ChatTab *const tab A_DYECMD_UNUSED) { if (!logger) return; + #ifdef DYECMD - if (resourceManager) - resourceManager->calcMemory(0); + ResourceManager::calcMemory(0); #else // DYECMD - int sz = 0; - if (resourceManager) - sz += resourceManager->calcMemory(0); + int sz = ResourceManager::calcMemory(0); if (tab) { // TRANSLATORS: memory usage chat message diff --git a/src/resources/resource.cpp b/src/resources/resource.cpp index 0d01f440c..e75df7b44 100644 --- a/src/resources/resource.cpp +++ b/src/resources/resource.cpp @@ -66,7 +66,7 @@ void Resource::decRef() if (mRefCount == 0 && !mNotCount) { // Warn the manager that this resource is no longer used. - resourceManager->release(this); + ResourceManager::release(this); } } diff --git a/src/resources/resource.h b/src/resources/resource.h index e9f7e0531..4b2f57229 100644 --- a/src/resources/resource.h +++ b/src/resources/resource.h @@ -32,17 +32,15 @@ */ class Resource notfinal : public MemoryCounter { - friend class ResourceManager; - public: /** * Constructor */ Resource() : MemoryCounter(), + mTimeStamp(0), mIdPath(), mSource(), - mTimeStamp(0), mRefCount(0), mProtected(false), #ifdef DEBUG_DUMP_LEAKS @@ -57,6 +55,11 @@ class Resource notfinal : public MemoryCounter A_DELETE_COPY(Resource) /** + * Destructor. + */ + virtual ~Resource(); + + /** * Increments the internal reference count. */ virtual void incRef(); @@ -76,6 +79,9 @@ class Resource notfinal : public MemoryCounter const std::string &getIdPath() const noexcept2 A_WARN_UNUSED { return mIdPath; } + void setIdPath(const std::string &path) + { mIdPath = path; } + /** * Return refCount for this resource. */ @@ -110,19 +116,15 @@ class Resource notfinal : public MemoryCounter { mDumped = n; } #endif // DEBUG_DUMP_LEAKS + time_t mTimeStamp; /**< Time at which the resource was orphaned. */ + #ifndef UNITTESTS protected: #endif // UNITTESTS - /** - * Destructor. - */ - virtual ~Resource(); - std::string mIdPath; /**< Path identifying this resource. */ std::string mSource; private: - time_t mTimeStamp; /**< Time at which the resource was orphaned. */ unsigned int mRefCount; /**< Reference count. */ bool mProtected; bool mNotCount; diff --git a/src/resources/resourcemanager/resourcemanager.cpp b/src/resources/resourcemanager/resourcemanager.cpp index aed4426b6..a5e5ebac5 100644 --- a/src/resources/resourcemanager/resourcemanager.cpp +++ b/src/resources/resourcemanager/resourcemanager.cpp @@ -46,20 +46,17 @@ #include "debug.h" -ResourceManager *resourceManager = nullptr; - -ResourceManager::ResourceManager() : - deletedSurfaces(), - mResources(), - mOrphanedResources(), - mDeletedResources(), - mOldestOrphan(0), - mDestruction(0) +namespace ResourceManager { - logger->log1("Initializing resource manager..."); -} -ResourceManager::~ResourceManager() +std::set<SDL_Surface*> deletedSurfaces; +Resources mResources; +Resources mOrphanedResources; +std::set<Resource*> mDeletedResources; +time_t mOldestOrphan = 0; +bool mDestruction = false; + +void deleteResourceManager() { mDestruction = true; mResources.insert(mOrphanedResources.begin(), mOrphanedResources.end()); @@ -171,26 +168,22 @@ ResourceManager::~ResourceManager() } clearDeleted(); clearScheduled(); + mDestruction = false; } -void ResourceManager::init() -{ - if (!resourceManager) - resourceManager = new ResourceManager; -} - -void ResourceManager::cleanUp(Resource *const res) +void cleanUp(Resource *const res) { if (!res) return; - if (res->mRefCount > 0) + const unsigned refCount = res->getRefCount(); + if (refCount > 0) { logger->log("ResourceManager::~ResourceManager() cleaning up %u " "reference%s to %s", - res->mRefCount, - (res->mRefCount == 1) ? "" : "s", - res->mIdPath.c_str()); + refCount, + (refCount == 1) ? "" : "s", + res->getIdPath().c_str()); } delete res; @@ -199,7 +192,7 @@ void ResourceManager::cleanUp(Resource *const res) #endif // DEBUG_LEAKS } -void ResourceManager::cleanProtected() +void cleanProtected() { ResourceIterator iter = mResources.begin(); while (iter != mResources.end()) @@ -222,7 +215,7 @@ void ResourceManager::cleanProtected() } } -bool ResourceManager::cleanOrphans(const bool always) +bool cleanOrphans(const bool always) { timeval tv; gettimeofday(&tv, nullptr); @@ -266,7 +259,7 @@ bool ResourceManager::cleanOrphans(const bool always) return status; } -void ResourceManager::logResource(const Resource *const res) +void logResource(const Resource *const res) { if (!res) return; @@ -278,7 +271,7 @@ void ResourceManager::logResource(const Resource *const res) const int count = image->getRefCount(); if (count) src.append(" ").append(toString(count)); - logger->log("resource(%s, %u) %s", res->mIdPath.c_str(), + logger->log("resource(%s, %u) %s", res->getIdPath().c_str(), image->getGLImage(), src.c_str()); } else @@ -287,15 +280,36 @@ void ResourceManager::logResource(const Resource *const res) const int count = res->getRefCount(); if (count > 0) src.append(" ").append(toString(count)); - logger->log("resource(%s) %s", res->mIdPath.c_str(), src.c_str()); + logger->log("resource(%s) %s", res->getIdPath().c_str(), src.c_str()); } #else // USE_OPENGL - logger->log("resource(%s)", res->mIdPath.c_str()); + logger->log("resource(%s)", res->getIdPath().c_str()); #endif // USE_OPENGL } -void ResourceManager::clearDeleted(const bool full) +void logResources(const std::string &msg) +{ + logger->log("start of resources: " + msg); + logger->log("resouces"); + FOR_EACH(ResourceIterator, it, mResources) + { + logResource((*it).second); + } + logger->log("orphaned resouces"); + FOR_EACH(ResourceIterator, it, mOrphanedResources) + { + logResource((*it).second); + } + logger->log("deleted resouces"); + FOR_EACH(std::set<Resource*>::iterator, it, mDeletedResources) + { + logResource(*it); + } + logger->log("end of resources"); +} + +void clearDeleted(const bool full) { bool status(true); logger->log1("clear deleted"); @@ -334,17 +348,16 @@ void ResourceManager::clearDeleted(const bool full) } } - -bool ResourceManager::addResource(const std::string &idPath, - Resource *const resource) +bool addResource(const std::string &idPath, + Resource *const resource) { if (resource) { resource->incRef(); - resource->mIdPath = idPath; + resource->setIdPath(idPath); #ifdef DEBUG_IMAGES logger->log("set name %p, %s", static_cast<void*>(resource), - resource->mIdPath.c_str()); + resource->getIdPath().c_str()); #endif // DEBUG_IMAGES mResources[idPath] = resource; @@ -353,21 +366,21 @@ bool ResourceManager::addResource(const std::string &idPath, return false; } -Resource *ResourceManager::getFromCache(const std::string &filename, - const int variant) +Resource *getFromCache(const std::string &filename, + const int variant) { std::stringstream ss; ss << filename << "[" << variant << "]"; return getFromCache(ss.str()); } -bool ResourceManager::isInCache(const std::string &idPath) const +bool isInCache(const std::string &idPath) { const ResourceCIterator &resIter = mResources.find(idPath); return (resIter != mResources.end() && resIter->second); } -Resource *ResourceManager::getTempResource(const std::string &idPath) +Resource *getTempResource(const std::string &idPath) { const ResourceCIterator &resIter = mResources.find(idPath); if (resIter != mResources.end()) @@ -379,7 +392,7 @@ Resource *ResourceManager::getTempResource(const std::string &idPath) return nullptr; } -Resource *ResourceManager::getFromCache(const std::string &idPath) +Resource *getFromCache(const std::string &idPath) { // Check if the id exists, and return the value if it does. ResourceIterator resIter = mResources.find(idPath); @@ -403,9 +416,9 @@ Resource *ResourceManager::getFromCache(const std::string &idPath) return nullptr; } -Resource *ResourceManager::get(const std::string &idPath, - generator fun, - const void *const data) +Resource *get(const std::string &idPath, + generator fun, + const void *const data) { #ifndef DISABLE_RESOURCE_CACHING Resource *resource = getFromCache(idPath); @@ -416,10 +429,10 @@ Resource *ResourceManager::get(const std::string &idPath, if (resource) { resource->incRef(); - resource->mIdPath = idPath; + resource->setIdPath(idPath); #ifdef DEBUG_IMAGES logger->log("set name %p, %s", static_cast<void*>(resource), - resource->mIdPath.c_str()); + resource->getIdPath().c_str()); #endif // DEBUG_IMAGES mResources[idPath] = resource; @@ -435,10 +448,10 @@ Resource *ResourceManager::get(const std::string &idPath, if (resource) { resource->incRef(); - resource->mIdPath = idPath; + resource->setIdPath(idPath); #ifdef DEBUG_IMAGES logger->log("set name %p, %s", static_cast<void*>(resource), - resource->mIdPath.c_str()); + resource->getIdPath().c_str()); #endif // DEBUG_IMAGES } else @@ -451,7 +464,7 @@ Resource *ResourceManager::get(const std::string &idPath, return resource; } -void ResourceManager::release(Resource *const res) +void release(Resource *const res) { if (!res || mDestruction) return; @@ -466,19 +479,19 @@ void ResourceManager::release(Resource *const res) return; } - ResourceIterator resIter = mResources.find(res->mIdPath); + ResourceIterator resIter = mResources.find(res->getIdPath()); if (resIter == mResources.end()) { reportAlways("no resource in cache: %s", - res->mIdPath.c_str()); + res->getIdPath().c_str()); delete res; return; } if (resIter->second != res) { reportAlways("in cache other image: %s", - res->mIdPath.c_str()); + res->getIdPath().c_str()); delete res; return; } @@ -499,7 +512,7 @@ void ResourceManager::release(Resource *const res) #endif // DISABLE_RESOURCE_CACHING } -void ResourceManager::moveToDeleted(Resource *const res) +void moveToDeleted(Resource *const res) { if (!res) return; @@ -509,7 +522,7 @@ void ResourceManager::moveToDeleted(Resource *const res) if (count == 1) logResource(res); res->decRef(); - ResourceIterator resIter = mResources.find(res->mIdPath); + ResourceIterator resIter = mResources.find(res->getIdPath()); if (resIter != mResources.end() && resIter->second == res) { mResources.erase(resIter); @@ -517,7 +530,7 @@ void ResourceManager::moveToDeleted(Resource *const res) } else { - resIter = mOrphanedResources.find(res->mIdPath); + resIter = mOrphanedResources.find(res->getIdPath()); if (resIter != mOrphanedResources.end() && resIter->second == res) { mOrphanedResources.erase(resIter); @@ -533,7 +546,7 @@ void ResourceManager::moveToDeleted(Resource *const res) } } -void ResourceManager::decRefDelete(Resource *const res) +void decRefDelete(Resource *const res) { if (!res) return; @@ -543,14 +556,14 @@ void ResourceManager::decRefDelete(Resource *const res) { logResource(res); - ResourceIterator resIter = mResources.find(res->mIdPath); + ResourceIterator resIter = mResources.find(res->getIdPath()); if (resIter != mResources.end() && resIter->second == res) { mResources.erase(resIter); } else { - resIter = mOrphanedResources.find(res->mIdPath); + resIter = mOrphanedResources.find(res->getIdPath()); if (resIter != mOrphanedResources.end() && resIter->second == res) mOrphanedResources.erase(resIter); } @@ -563,55 +576,52 @@ void ResourceManager::decRefDelete(Resource *const res) } } -void ResourceManager::deleteInstance() +void deleteInstance() { #ifdef DUMP_LEAKED_RESOURCES - if (resourceManager) - { - logger->log1("clean orphans start"); - resourceManager->cleanProtected(); - while (resourceManager->cleanOrphans(true)) - continue; - logger->log1("clean orphans end"); - ResourceIterator iter = resourceManager->mResources.begin(); + logger->log1("clean orphans start"); + ResourceManager::cleanProtected(); + while (ResourceManager::cleanOrphans(true)) + continue; + logger->log1("clean orphans end"); + ResourceIterator iter = ResourceManager::mResources.begin(); #ifdef UNITTESTS - bool status(false); + bool status(false); #endif // UNITTESTS - while (iter != resourceManager->mResources.end()) + while (iter != ResourceManager::mResources.end()) + { + const Resource *const res = iter->second; + if (res) { - const Resource *const res = iter->second; - if (res) + if (res->getRefCount()) { - if (res->getRefCount()) - { - logger->log(std::string("ResourceLeak: ").append( - res->getIdPath()).append(" (").append(toString( - res->getRefCount())).append(")")); + logger->log(std::string("ResourceLeak: ").append( + res->getIdPath()).append(" (").append(toString( + res->getRefCount())).append(")")); #ifdef UNITTESTS - status = true; + status = true; #endif // UNITTESTS - } } - ++iter; } + ++iter; + } #ifdef UNITTESTS - if (status) - reportAlways("Found leaked resources."); + if (status) + reportAlways("Found leaked resources."); #endif // UNITTESTS - } #endif // DUMP_LEAKED_RESOURCES - delete2(resourceManager); + deleteResourceManager(); } -void ResourceManager::scheduleDelete(SDL_Surface *const surface) +void scheduleDelete(SDL_Surface *const surface) { deletedSurfaces.insert(surface); } -void ResourceManager::clearScheduled() +void clearScheduled() { BLOCK_START("ResourceManager::clearScheduled") FOR_EACH (std::set<SDL_Surface*>::iterator, i, deletedSurfaces) @@ -620,16 +630,16 @@ void ResourceManager::clearScheduled() BLOCK_END("ResourceManager::clearScheduled") } -void ResourceManager::clearCache() +void clearCache() { cleanProtected(); while (cleanOrphans(true)) continue; } -int ResourceManager::calcMemoryLocal() const +int calcMemoryLocal() { - int sz = sizeof(ResourceManager); + int sz = 24; FOR_EACH (std::set<SDL_Surface*>::iterator, it, deletedSurfaces) { sz += memoryManager.getSurfaceSize(*it); @@ -637,7 +647,7 @@ int ResourceManager::calcMemoryLocal() const return sz; } -int ResourceManager::calcMemoryChilds(const int level) const +int calcMemoryChilds(const int level) { int sz = 0; FOR_EACH (ResourceCIterator, it, mResources) @@ -656,3 +666,38 @@ int ResourceManager::calcMemoryChilds(const int level) const } return sz; } + +int calcMemory(const int level) +{ + const int sumLocal = calcMemoryLocal(); + const int sumChilds = calcMemoryChilds(0); + memoryManager.printMemory("resource manager", + level, + sumLocal, + sumChilds); + return sumLocal + sumChilds; +} + +int size() noexcept2 +{ + return CAST_S32(mResources.size()); +} + +#if defined(DEBUG_DUMP_LEAKS) || defined(UNITTESTS) +Resources &getResources() +{ + return mResources; +} + +Resources &getOrphanedResources() +{ + return mOrphanedResources; +} + +const std::set<Resource*> &getDeletedResources() +{ + return mDeletedResources; +} +#endif // defined(DEBUG_DUMP_LEAKS) || defined(UNITTESTS) + +} // namespace ResourceManager diff --git a/src/resources/resourcemanager/resourcemanager.h b/src/resources/resourcemanager/resourcemanager.h index 6fff83df0..f279b2b7c 100644 --- a/src/resources/resourcemanager/resourcemanager.h +++ b/src/resources/resourcemanager/resourcemanager.h @@ -23,8 +23,6 @@ #ifndef RESOURCES_RESOURCEMANAGER_RESOURCEMANAGER_H #define RESOURCES_RESOURCEMANAGER_RESOURCEMANAGER_H -#include "resources/memorycounter.h" - #include <map> #include <set> @@ -35,124 +33,97 @@ class Resource; struct SDL_Surface; struct SDL_RWops; -/** - * A class for loading and managing resources. - */ -class ResourceManager final : public MemoryCounter +namespace ResourceManager { - friend class Resource; - - public: - typedef Resource *(*loader)(SDL_RWops *rw, - const std::string &name); - typedef Resource *(&generator)(const void *const data); - - ResourceManager(); - - A_DELETE_COPY(ResourceManager) - - /** - * Destructor. Cleans up remaining resources, warning about resources - * that were still referenced. - */ - ~ResourceManager(); + typedef Resource *(*loader)(SDL_RWops *rw, + const std::string &name); + typedef Resource *(&generator)(const void *const data); - /** - * Creates a resource and adds it to the resource map. - * - * @param idPath The resource identifier path. - * @param fun A function for generating the resource. - * @param data Extra parameters for the generator. - * @return A valid resource or <code>NULL</code> if the resource could - * not be generated. - */ - Resource *get(const std::string &idPath, - generator fun, - const void *const data) A_WARN_UNUSED; + typedef std::map<std::string, Resource*> Resources; + typedef Resources::iterator ResourceIterator; + typedef Resources::const_iterator ResourceCIterator; - Resource *getFromCache(const std::string &idPath) A_WARN_UNUSED; + /** + * Cleans up remaining resources, warning about resources + * that were still referenced. + */ + void deleteResourceManager(); - Resource *getFromCache(const std::string &filename, - const int variant) A_WARN_UNUSED; + /** + * Creates a resource and adds it to the resource map. + * + * @param idPath The resource identifier path. + * @param fun A function for generating the resource. + * @param data Extra parameters for the generator. + * @return A valid resource or <code>NULL</code> if the resource could + * not be generated. + */ + Resource *get(const std::string &idPath, + generator fun, + const void *const data) A_WARN_UNUSED; - bool addResource(const std::string &idPath, - Resource *const resource); + Resource *getFromCache(const std::string &idPath) A_WARN_UNUSED; - /** - * Releases a resource, placing it in the set of orphaned resources. - */ - void release(Resource *const res); + Resource *getFromCache(const std::string &filename, + const int variant) A_WARN_UNUSED; - void clearDeleted(const bool full = true); + bool addResource(const std::string &idPath, + Resource *const resource); - void decRefDelete(Resource *const res); + /** + * Releases a resource, placing it in the set of orphaned resources. + */ + void release(Resource *const res); - /** - * Move resource to deleted resources list. - */ - void moveToDeleted(Resource *const res); + void clearDeleted(const bool full = true); - void scheduleDelete(SDL_Surface *const surface); + void decRefDelete(Resource *const res); - void clearScheduled(); + /** + * Move resource to deleted resources list. + */ + void moveToDeleted(Resource *const res); - /** - * Deletes the class instance if it exists. - */ - static void deleteInstance(); + void scheduleDelete(SDL_Surface *const surface); - int size() const noexcept2 A_WARN_UNUSED - { return CAST_S32(mResources.size()); } + void clearScheduled(); - typedef std::map<std::string, Resource*> Resources; - typedef Resources::iterator ResourceIterator; - typedef Resources::const_iterator ResourceCIterator; + /** + * Deletes the class instance if it exists. + */ + void deleteInstance(); -#ifdef DEBUG_DUMP_LEAKS - Resources* getResources() A_WARN_UNUSED - { return &mResources; } + int size() noexcept2 A_WARN_UNUSED; - Resources* getOrphanedResources() A_WARN_UNUSED - { return &mOrphanedResources; } -#endif // DEBUG_DUMP_LEAKS +#if defined(DEBUG_DUMP_LEAKS) || defined(UNITTESTS) + Resources &getResources() A_WARN_UNUSED; - bool cleanOrphans(const bool always = false); + Resources &getOrphanedResources() A_WARN_UNUSED; - void cleanProtected(); + const std::set<Resource*> &getDeletedResources() A_WARN_UNUSED; +#endif // defined(DEBUG_DUMP_LEAKS) || defined(UNITTESTS) - bool isInCache(const std::string &idPath) const A_WARN_UNUSED; + bool cleanOrphans(const bool always = false); - Resource *getTempResource(const std::string &idPath) A_WARN_UNUSED; + void cleanProtected(); - void clearCache(); + bool isInCache(const std::string &idPath) A_WARN_UNUSED; - int calcMemoryLocal() const override final; + Resource *getTempResource(const std::string &idPath) A_WARN_UNUSED; - int calcMemoryChilds(const int level) const override final; + void clearCache(); - std::string getCounterName() const override final - { return "ResourceManager"; } + int calcMemoryLocal(); - static void init(); + int calcMemoryChilds(const int level); -#ifndef UNITTESTS - private: -#endif // UNITTESTS - /** - * Deletes the resource after logging a cleanup message. - */ - static void cleanUp(Resource *const resource); + int calcMemory(const int level); - static void logResource(const Resource *const res); + void cleanUp(Resource *const resource); - std::set<SDL_Surface*> deletedSurfaces; - Resources mResources; - Resources mOrphanedResources; - std::set<Resource*> mDeletedResources; - time_t mOldestOrphan; - bool mDestruction; -}; + void logResource(const Resource *const res); -extern ResourceManager *resourceManager; + void logResources(const std::string &msg); +} // namespace ResourceManager #endif // RESOURCES_RESOURCEMANAGER_RESOURCEMANAGER_H diff --git a/src/resources/resourcemanager/resourcemanager_unittest.cc b/src/resources/resourcemanager/resourcemanager_unittest.cc index c700bfbab..8c8d72868 100644 --- a/src/resources/resourcemanager/resourcemanager_unittest.cc +++ b/src/resources/resourcemanager/resourcemanager_unittest.cc @@ -20,6 +20,9 @@ #include "catch.hpp" #include "client.h" +#include "configmanager.h" +#include "configuration.h" +#include "dirs.h" #include "logger.h" #include "graphicsmanager.h" @@ -27,6 +30,8 @@ #include "fs/virtfs/virtfs.h" +#include "gui/gui.h" + #include "resources/sdlimagehelper.h" #include "resources/resourcemanager/resourcemanager.h" @@ -84,6 +89,15 @@ namespace } // namespace +TEST_CASE("resourcemanager leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("resourcemanager", "resourcemanager") { setEnv("SDL_VIDEODRIVER", "dummy"); @@ -92,8 +106,6 @@ TEST_CASE("resourcemanager", "resourcemanager") XML::initXML(); SDL_Init(SDL_INIT_VIDEO); logger = new Logger(); - ResourceManager::init(); - ResourceManager *safeResman = resourceManager; VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -108,22 +120,40 @@ TEST_CASE("resourcemanager", "resourcemanager") #endif // USE_SDL2 ActorSprite::load(); - resourceManager = new ResourceManager; + + Dirs::initRootDir(); + Dirs::initHomeDir(); + +// ConfigManager::initConfiguration(); +// getConfigDefaults2(config.getDefaultValues()); + + while (ResourceManager::cleanOrphans(true)) + continue; + testResouceCounter = 0; + const size_t resSize = ResourceManager::getResources().size(); + + SECTION("resourcemanager get 0") + { + REQUIRE(testResouceCounter == 0); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + } SECTION("resourcemanager get 1") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); REQUIRE(res->getRefCount() == 0); } @@ -131,18 +161,18 @@ TEST_CASE("resourcemanager", "resourcemanager") SECTION("resourcemanager get 2") { TestLoader rl = { "test1" }; - Resource *const res = resourceManager->get("test1", + Resource *const res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); res->setSource("source 1"); REQUIRE(res->getSource() == "source 1"); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - Resource *const res2 = resourceManager->get("test1", + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + Resource *const res2 = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res2 != nullptr); REQUIRE(testResouceCounter == 1); @@ -150,10 +180,10 @@ TEST_CASE("resourcemanager", "resourcemanager") REQUIRE(res2->getRefCount() == 2); REQUIRE(res->getIdPath() == res2->getIdPath()); REQUIRE(res2->getSource() == "source 1"); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res2); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res2); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); res2->decRef(); REQUIRE(res->getRefCount() == 0); @@ -162,53 +192,53 @@ TEST_CASE("resourcemanager", "resourcemanager") SECTION("resourcemanager get 3") { TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->setSource("source 1"); res->decRef(); - REQUIRE(resourceManager->mResources.size() == 0); - REQUIRE(resourceManager->mOrphanedResources["test1"] == res); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); REQUIRE(res->getRefCount() == 0); } SECTION("resourcemanager get 4") { TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->setSource("source 1"); res->decRef(); REQUIRE(res->getRefCount() == 0); - REQUIRE(resourceManager->mResources.size() == 0); - REQUIRE(resourceManager->mOrphanedResources["test1"] == res); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); - Resource *const res2 = resourceManager->get("test1", + Resource *const res2 = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res2 != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res2->getRefCount() == 1); REQUIRE(res->getIdPath() == res2->getIdPath()); REQUIRE(res2->getSource() == "source 1"); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res2); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res2); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res2->decRef(); REQUIRE(res->getRefCount() == 0); } @@ -216,43 +246,43 @@ TEST_CASE("resourcemanager", "resourcemanager") SECTION("resourcemanager get 5") { TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); - REQUIRE(resourceManager->isInCache("test1") == true); + REQUIRE(ResourceManager::isInCache("test1") == true); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->setSource("source 1"); res->decRef(); REQUIRE(res->getRefCount() == 0); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.size() == 1); - REQUIRE(resourceManager->mOrphanedResources["test1"] == res); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().size() == 1); + REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); sleep(33); - resourceManager->cleanOrphans(); - REQUIRE(resourceManager->isInCache("test1") == false); + ResourceManager::cleanOrphans(); + REQUIRE(ResourceManager::isInCache("test1") == false); REQUIRE(testResouceCounter == 0); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); - Resource *const res2 = resourceManager->get("test1", + Resource *const res2 = ResourceManager::get("test1", TestLoader::load, &rl); - REQUIRE(resourceManager->isInCache("test1") == true); + REQUIRE(ResourceManager::isInCache("test1") == true); REQUIRE(res2 != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res2->getRefCount() == 1); REQUIRE(res2->getSource() == ""); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res2); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res2); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res2->decRef(); REQUIRE(res2->getRefCount() == 0); } @@ -260,28 +290,28 @@ TEST_CASE("resourcemanager", "resourcemanager") SECTION("resourcemanager get 6") { TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); res->setSource("source 1"); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); - Resource *const res2 = resourceManager->get("test2", + Resource *const res2 = ResourceManager::get("test2", TestLoader::load, &rl); REQUIRE(res2 != nullptr); REQUIRE(testResouceCounter == 2); REQUIRE(res2->getRefCount() == 1); REQUIRE(res2->getSource() == ""); - REQUIRE(resourceManager->mResources.size() == 2); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mResources["test2"] == res2); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 2 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getResources()["test2"] == res2); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); res2->decRef(); REQUIRE(res->getRefCount() == 0); @@ -291,25 +321,25 @@ TEST_CASE("resourcemanager", "resourcemanager") SECTION("resourcemanager getFromCache 1") { TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - Resource *const res2 = resourceManager->getFromCache("test1"); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + Resource *const res2 = ResourceManager::getFromCache("test1"); REQUIRE(res2 != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res2->getRefCount() == 2); REQUIRE(res == res2); REQUIRE(res2->getSource() == ""); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res2); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res2); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); res->decRef(); REQUIRE(res->getRefCount() == 0); @@ -318,27 +348,27 @@ TEST_CASE("resourcemanager", "resourcemanager") SECTION("resourcemanager getFromCache 2") { TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); REQUIRE(res->getRefCount() == 0); - Resource *const res2 = resourceManager->getFromCache("test1"); + Resource *const res2 = ResourceManager::getFromCache("test1"); REQUIRE(res2 != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res2->getRefCount() == 1); REQUIRE(res == res2); REQUIRE(res2->getSource() == ""); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res2); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res2); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); REQUIRE(res->getRefCount() == 0); } @@ -346,31 +376,31 @@ TEST_CASE("resourcemanager", "resourcemanager") SECTION("resourcemanager getFromCache 3") { TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); res->setSource("source 1"); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); - Resource *const res2 = resourceManager->get("test2", + Resource *const res2 = ResourceManager::get("test2", TestLoader::load, &rl); REQUIRE(res2 != nullptr); REQUIRE(testResouceCounter == 2); REQUIRE(res2->getRefCount() == 1); REQUIRE(res2->getSource() == ""); - REQUIRE(resourceManager->mResources.size() == 2); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mResources["test2"] == res2); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - - Resource *const resC = resourceManager->getFromCache("test1"); - Resource *const res2C = resourceManager->getFromCache("test2"); + REQUIRE(ResourceManager::getResources().size() == 2 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getResources()["test2"] == res2); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + + Resource *const resC = ResourceManager::getFromCache("test1"); + Resource *const res2C = ResourceManager::getFromCache("test2"); REQUIRE(resC != nullptr); REQUIRE(res2C != nullptr); REQUIRE(testResouceCounter == 2); @@ -378,11 +408,11 @@ TEST_CASE("resourcemanager", "resourcemanager") REQUIRE(res2 == res2C); REQUIRE(resC->getRefCount() == 2); REQUIRE(res2C->getRefCount() == 2); - REQUIRE(resourceManager->mResources.size() == 2); - REQUIRE(resourceManager->mResources["test1"] == resC); - REQUIRE(resourceManager->mResources["test2"] == res2C); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 2 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == resC); + REQUIRE(ResourceManager::getResources()["test2"] == res2C); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); res->decRef(); @@ -397,13 +427,13 @@ TEST_CASE("resourcemanager", "resourcemanager") REQUIRE(testResouceCounter == 0); Resource *res = new TestResource(); REQUIRE(testResouceCounter == 1); - resourceManager->addResource("test1", res); + ResourceManager::addResource("test1", res); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); REQUIRE(res->getRefCount() == 0); } @@ -412,18 +442,18 @@ TEST_CASE("resourcemanager", "resourcemanager") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - REQUIRE(resourceManager->isInCache("test1") == false); - Resource *res = resourceManager->get("test1", + REQUIRE(ResourceManager::isInCache("test1") == false); + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); - REQUIRE(resourceManager->isInCache("test1") == true); - REQUIRE(resourceManager->isInCache("test2") == false); + REQUIRE(ResourceManager::isInCache("test1") == true); + REQUIRE(ResourceManager::isInCache("test2") == false); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); REQUIRE(res->getRefCount() == 0); } @@ -431,27 +461,27 @@ TEST_CASE("resourcemanager", "resourcemanager") SECTION("resourcemanager getTempResource 1") { TestLoader rl = { "test1" }; - REQUIRE(resourceManager->getTempResource("test1") == nullptr); - Resource *res = resourceManager->get("test1", + REQUIRE(ResourceManager::getTempResource("test1") == nullptr); + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(res != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - Resource *const res2 = resourceManager->getTempResource("test1"); - REQUIRE(resourceManager->getTempResource("test2") == nullptr); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + Resource *const res2 = ResourceManager::getTempResource("test1"); + REQUIRE(ResourceManager::getTempResource("test2") == nullptr); REQUIRE(res2 != nullptr); REQUIRE(testResouceCounter == 1); REQUIRE(res2->getRefCount() == 1); REQUIRE(res == res2); REQUIRE(res2->getSource() == ""); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res2); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res2); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); REQUIRE(res->getRefCount() == 0); } @@ -460,42 +490,42 @@ TEST_CASE("resourcemanager", "resourcemanager") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - resourceManager->moveToDeleted(res); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + ResourceManager::moveToDeleted(res); REQUIRE(testResouceCounter == 0); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); } SECTION("resourcemanager moveToDeleted 2") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); res->incRef(); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 2); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - resourceManager->moveToDeleted(res); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + ResourceManager::moveToDeleted(res); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.size() == 1); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().size() == 1); res->decRef(); } @@ -503,96 +533,96 @@ TEST_CASE("resourcemanager", "resourcemanager") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 0); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.size() == 1); - REQUIRE(resourceManager->mOrphanedResources["test1"] == res); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().size() == 1); + REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); - resourceManager->moveToDeleted(res); + ResourceManager::moveToDeleted(res); REQUIRE(testResouceCounter == 0); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); } SECTION("resourcemanager decRefDelete 1") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); - resourceManager->decRefDelete(res); + ResourceManager::decRefDelete(res); REQUIRE(testResouceCounter == 0); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); } SECTION("resourcemanager cleanUp 1") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); - resourceManager->cleanUp(res); + ResourceManager::cleanUp(res); REQUIRE(testResouceCounter == 0); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.size() == 1); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - resourceManager->mOrphanedResources.clear(); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().size() == 1); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + ResourceManager::getOrphanedResources().clear(); } SECTION("resourcemanager cleanProtected 1") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->setProtected(true); - resourceManager->cleanProtected(); + ResourceManager::cleanProtected(); REQUIRE(testResouceCounter == 1); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.size() == 1); - REQUIRE(resourceManager->mOrphanedResources["test1"] == res); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().size() == 1); + REQUIRE(ResourceManager::getOrphanedResources()["test1"] == res); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); res->decRef(); } @@ -600,29 +630,29 @@ TEST_CASE("resourcemanager", "resourcemanager") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); res->incRef(); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 2); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - resourceManager->moveToDeleted(res); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + ResourceManager::moveToDeleted(res); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.size() == 1); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().size() == 1); - resourceManager->clearDeleted(); + ResourceManager::clearDeleted(); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.size() == 1); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().size() == 1); res->decRef(); } @@ -630,36 +660,43 @@ TEST_CASE("resourcemanager", "resourcemanager") { REQUIRE(testResouceCounter == 0); TestLoader rl = { "test1" }; - Resource *res = resourceManager->get("test1", + Resource *res = ResourceManager::get("test1", TestLoader::load, &rl); res->incRef(); REQUIRE(testResouceCounter == 1); REQUIRE(res != nullptr); REQUIRE(res->getRefCount() == 2); - REQUIRE(resourceManager->mResources.size() == 1); - REQUIRE(resourceManager->mResources["test1"] == res); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); - resourceManager->moveToDeleted(res); + REQUIRE(ResourceManager::getResources().size() == 1 + resSize); + REQUIRE(ResourceManager::getResources()["test1"] == res); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); + ResourceManager::moveToDeleted(res); REQUIRE(testResouceCounter == 1); REQUIRE(res->getRefCount() == 1); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.size() == 1); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().size() == 1); res->decRef(); - resourceManager->clearDeleted(); + ResourceManager::clearDeleted(); REQUIRE(testResouceCounter == 0); - REQUIRE(resourceManager->mResources.empty() == true); - REQUIRE(resourceManager->mOrphanedResources.empty() == true); - REQUIRE(resourceManager->mDeletedResources.empty() == true); + REQUIRE(ResourceManager::getResources().size() == 0 + resSize); + REQUIRE(ResourceManager::getOrphanedResources().empty() == true); + REQUIRE(ResourceManager::getDeletedResources().empty() == true); } - delete resourceManager; - resourceManager = safeResman; delete2(client); VirtFs::unmountDirSilent("data"); VirtFs::unmountDirSilent("../data"); delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("resourcemanager leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/resources/sprite/animatedsprite.cpp b/src/resources/sprite/animatedsprite.cpp index 960a5ea00..3e9250e13 100644 --- a/src/resources/sprite/animatedsprite.cpp +++ b/src/resources/sprite/animatedsprite.cpp @@ -90,7 +90,7 @@ AnimatedSprite *AnimatedSprite::delayedLoad(const std::string &restrict { if (!mEnableCache) return load(filename, variant); - Resource *restrict const res = resourceManager->getFromCache( + Resource *restrict const res = ResourceManager::getFromCache( filename, variant); if (res) { diff --git a/src/resources/sprite/animatedsprite_unittest.cc b/src/resources/sprite/animatedsprite_unittest.cc index d17177a6d..f48b10b4b 100644 --- a/src/resources/sprite/animatedsprite_unittest.cc +++ b/src/resources/sprite/animatedsprite_unittest.cc @@ -54,6 +54,15 @@ #include "debug.h" +TEST_CASE("AnimatedSprite leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("AnimatedSprite tests", "animatedsprite") { setEnv("SDL_VIDEODRIVER", "dummy"); @@ -63,7 +72,6 @@ TEST_CASE("AnimatedSprite tests", "animatedsprite") XML::initXML(); SDL_Init(SDL_INIT_VIDEO); logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); mainGraphics = new SDLGraphics; @@ -196,3 +204,12 @@ TEST_CASE("AnimatedSprite tests", "animatedsprite") delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("AnimatedSprite leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/utils/chatutils_unittest.cc b/src/utils/chatutils_unittest.cc index b5b368f31..d613665dc 100644 --- a/src/utils/chatutils_unittest.cc +++ b/src/utils/chatutils_unittest.cc @@ -50,13 +50,21 @@ #include "debug.h" +TEST_CASE("chatutils leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("chatutils replaceVars") { client = new Client; XML::initXML(); SDL_Init(SDL_INIT_VIDEO); logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); theme = new Theme; @@ -260,3 +268,12 @@ TEST_CASE("chatutils textToMe") REQUIRE(textToMe("*") == "***"); REQUIRE(textToMe("test line") == "*test line*"); } + +TEST_CASE("chatutils leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/utils/stringutils_unittest.cc b/src/utils/stringutils_unittest.cc index d93e7a007..9d3285ccc 100644 --- a/src/utils/stringutils_unittest.cc +++ b/src/utils/stringutils_unittest.cc @@ -1474,7 +1474,6 @@ TEST_CASE("stringuntils timeDiffToString") TEST_CASE("stringuntils replaceItemLinks") { logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); VirtFs::mountDirSilent("data/test", Append_false); diff --git a/src/utils/translation/poparser_unittest.cc b/src/utils/translation/poparser_unittest.cc index 3d7ac2aaa..7ba006f82 100644 --- a/src/utils/translation/poparser_unittest.cc +++ b/src/utils/translation/poparser_unittest.cc @@ -46,13 +46,21 @@ #include "debug.h" +TEST_CASE("PoParser leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("PoParser tests", "PoParser") { setEnv("SDL_VIDEODRIVER", "dummy"); client = new Client; logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -137,3 +145,12 @@ TEST_CASE("PoParser tests", "PoParser") delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("PoParser leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/utils/xml_unittest.cc b/src/utils/xml_unittest.cc index a52c5f522..6fd9a5e0a 100644 --- a/src/utils/xml_unittest.cc +++ b/src/utils/xml_unittest.cc @@ -42,18 +42,29 @@ #include "resources/sdlimagehelper.h" +#include "resources/sprite/animatedsprite.h" + #include "resources/resourcemanager/resourcemanager.h" #include "debug.h" +TEST_CASE("xml test1") +{ + logger = new Logger(); + REQUIRE(client == nullptr); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("xml doc") { setEnv("SDL_VIDEODRIVER", "dummy"); + logger = new Logger(); client = new Client; XML::initXML(); - logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); mainGraphics = new SDLGraphics; @@ -371,9 +382,19 @@ TEST_CASE("xml doc") delete2(theme); delete2(client); + ResourceManager::deleteInstance(); VirtFs::unmountDirSilent("data"); VirtFs::unmountDirSilent("../data"); delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("xml test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} diff --git a/src/utils/xmlutils_unittest.cc b/src/utils/xmlutils_unittest.cc index 1a9796b8e..4752eaa53 100644 --- a/src/utils/xmlutils_unittest.cc +++ b/src/utils/xmlutils_unittest.cc @@ -28,6 +28,8 @@ #include "fs/virtfs/virtfs.h" +#include "gui/gui.h" + #include "utils/delete2.h" #include "render/sdlgraphics.h" @@ -38,12 +40,20 @@ #include "debug.h" +TEST_CASE("xmlutils leak test1") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} + TEST_CASE("xmlutils readXmlIntVector 1") { client = new Client; XML::initXML(); logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -89,7 +99,6 @@ TEST_CASE("xmlutils readXmlStringMap 1") client = new Client; XML::initXML(); logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); @@ -134,7 +143,6 @@ TEST_CASE("xmlutils readXmlIntMap 1") client = new Client; XML::initXML(); logger = new Logger(); - ResourceManager::init(); VirtFs::mountDirSilent("data", Append_false); VirtFs::mountDirSilent("../data", Append_false); VirtFs::mountDirSilent("data/test", Append_false); @@ -177,3 +185,12 @@ TEST_CASE("xmlutils readXmlIntMap 1") delete2(logger); // VirtFs::deinit(); } + +TEST_CASE("xmlutils leak test2") +{ + logger = new Logger(); + REQUIRE(gui == nullptr); + ResourceManager::cleanOrphans(true); + ResourceManager::deleteInstance(); + delete2(logger); +} |