From df6ce90c84710b3cc8faa909090a2f8345d57af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 18 May 2017 12:52:03 +0200 Subject: [PATCH] When what we need is a shared_ptr, we should just go ahead and use one. One of the top crashes reported in the Play Console is a ManagedTexture crash, so seeing if this might help. --- UI/BackgroundAudio.cpp | 2 +- UI/CwCheatScreen.cpp | 2 +- UI/EmuScreen.cpp | 3 +-- UI/GameInfoCache.cpp | 20 +++++++++++--------- UI/GameInfoCache.h | 18 ++++++++---------- UI/GameScreen.cpp | 22 +++++++++++----------- UI/MainScreen.cpp | 7 +++---- UI/MiscScreens.cpp | 7 +++---- UI/NativeApp.cpp | 5 ++--- UI/PauseScreen.cpp | 15 ++++++--------- UI/PauseScreen.h | 3 ++- UI/SavedataScreen.cpp | 8 ++++---- UI/Store.cpp | 20 ++++++++------------ UI/TextureUtil.cpp | 14 +++++++------- UI/TextureUtil.h | 6 ++++-- android/jni/app-android.cpp | 2 +- 16 files changed, 73 insertions(+), 81 deletions(-) diff --git a/UI/BackgroundAudio.cpp b/UI/BackgroundAudio.cpp index 8bd57caa8a21..261f4af5d2e7 100644 --- a/UI/BackgroundAudio.cpp +++ b/UI/BackgroundAudio.cpp @@ -228,7 +228,7 @@ int PlayBackgroundAudio() { if (!g_gameInfoCache) return 0; // race condition? - GameInfo *gameInfo = g_gameInfoCache->GetInfo(NULL, bgGamePath, GAMEINFO_WANTSND); + std::shared_ptr gameInfo = g_gameInfoCache->GetInfo(NULL, bgGamePath, GAMEINFO_WANTSND); if (!gameInfo) return 0; diff --git a/UI/CwCheatScreen.cpp b/UI/CwCheatScreen.cpp index 891e64cf78eb..edbcdfe92657 100644 --- a/UI/CwCheatScreen.cpp +++ b/UI/CwCheatScreen.cpp @@ -49,7 +49,7 @@ CwCheatScreen::CwCheatScreen(std::string gamePath) } void CwCheatScreen::CreateCodeList() { - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); if (info && info->paramSFOLoaded) { gameTitle = info->paramSFO.GetValueString("DISC_ID"); } diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index e05139d90446..e6d8a6b7aa8c 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -146,7 +146,7 @@ void EmuScreen::bootGame(const std::string &filename) { SetBackgroundAudioGame(""); //pre-emptive loading of game specific config if possible, to get all the settings - GameInfo *info = g_gameInfoCache->GetInfo(NULL, filename, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(nullptr, filename, 0); if (info && !info->id.empty()) { g_Config.loadGameConfig(info->id); } @@ -210,7 +210,6 @@ void EmuScreen::bootGame(const std::string &filename) { coreParam.pixelWidth = pixel_xres; coreParam.pixelHeight = pixel_yres; - std::string error_string; if (!PSP_InitStart(coreParam, &error_string)) { bootPending_ = false; diff --git a/UI/GameInfoCache.cpp b/UI/GameInfoCache.cpp index 36609edda512..7041dab87f0b 100644 --- a/UI/GameInfoCache.cpp +++ b/UI/GameInfoCache.cpp @@ -358,7 +358,7 @@ static bool ReadVFSToString(const char *filename, std::string *contents, std::mu class GameInfoWorkItem : public PrioritizedWorkQueueItem { public: - GameInfoWorkItem(const std::string &gamePath, GameInfo *info) + GameInfoWorkItem(const std::string &gamePath, std::shared_ptr &info) : gamePath_(gamePath), info_(info) { } @@ -617,7 +617,7 @@ class GameInfoWorkItem : public PrioritizedWorkQueueItem { private: std::string gamePath_; - GameInfo *info_; + std::shared_ptr info_; DISALLOW_COPY_AND_ASSIGN(GameInfoWorkItem); }; @@ -676,7 +676,7 @@ void GameInfoCache::PurgeType(IdentifiedFileType fileType) { } } -void GameInfoCache::WaitUntilDone(GameInfo *info) { +void GameInfoCache::WaitUntilDone(std::shared_ptr &info) { while (info->IsPending()) { if (gameInfoWQ_->WaitUntilDone(false)) { // A true return means everything finished, so bail out. @@ -690,12 +690,12 @@ void GameInfoCache::WaitUntilDone(GameInfo *info) { // Runs on the main thread. -GameInfo *GameInfoCache::GetInfo(Draw::DrawContext *draw, const std::string &gamePath, int wantFlags) { - GameInfo *info = nullptr; +std::shared_ptr GameInfoCache::GetInfo(Draw::DrawContext *draw, const std::string &gamePath, int wantFlags) { + std::shared_ptr info; auto iter = info_.find(gamePath); if (iter != info_.end()) { - info = iter->second.get(); + info = iter->second; } // If wantFlags don't match, we need to start over. We'll just queue the work item again. @@ -714,7 +714,7 @@ GameInfo *GameInfoCache::GetInfo(Draw::DrawContext *draw, const std::string &gam } if (!info) { - info = new GameInfo(); + info = std::make_shared(); } if (info->IsWorking()) { @@ -732,11 +732,13 @@ GameInfo *GameInfoCache::GetInfo(Draw::DrawContext *draw, const std::string &gam GameInfoWorkItem *item = new GameInfoWorkItem(gamePath, info); gameInfoWQ_->Add(item); - info_[gamePath] = std::unique_ptr(info); + // Don't re-insert if we already have it. + if (info_.find(gamePath) == info_.end()) + info_[gamePath] = std::shared_ptr(info); return info; } -void GameInfoCache::SetupTexture(GameInfo *info, Draw::DrawContext *thin3d, GameInfoTex &tex) { +void GameInfoCache::SetupTexture(std::shared_ptr &info, Draw::DrawContext *thin3d, GameInfoTex &tex) { using namespace Draw; if (tex.data.size()) { if (!tex.texture) { diff --git a/UI/GameInfoCache.h b/UI/GameInfoCache.h index 616bf8947b7c..14e290a0409b 100644 --- a/UI/GameInfoCache.h +++ b/UI/GameInfoCache.h @@ -67,7 +67,7 @@ struct GameInfoTex { } } std::string data; - ManagedTexture *texture = nullptr; + std::unique_ptr texture; // The time at which the Icon and the BG were loaded. // Can be useful to fade them in smoothly once they appear. double timeLoaded = 0.0; @@ -78,10 +78,7 @@ struct GameInfoTex { data.clear(); dataLoaded = false; } - if (texture) { - delete texture; - texture = nullptr; - } + texture.reset(nullptr); } private: DISALLOW_COPY_AND_ASSIGN(GameInfoTex); @@ -171,20 +168,21 @@ class GameInfoCache { // but filled in later asynchronously in the background. So keep calling this, // redrawing the UI often. Only set flags to GAMEINFO_WANTBG or WANTSND if you really want them // because they're big. bgTextures and sound may be discarded over time as well. - GameInfo *GetInfo(Draw::DrawContext *draw, const std::string &gamePath, int wantFlags); + std::shared_ptr GetInfo(Draw::DrawContext *draw, const std::string &gamePath, int wantFlags); void FlushBGs(); // Gets rid of all BG textures. Also gets rid of bg sounds. PrioritizedWorkQueue *WorkQueue() { return gameInfoWQ_; } - void WaitUntilDone(GameInfo *info); + void WaitUntilDone(std::shared_ptr &info); private: void Init(); void Shutdown(); - void SetupTexture(GameInfo *info, Draw::DrawContext *draw, GameInfoTex &tex); + void SetupTexture(std::shared_ptr &info, Draw::DrawContext *draw, GameInfoTex &tex); - // Maps ISO path to info. - std::map > info_; + // Maps ISO path to info. Need to use shared_ptr as we can return these pointers - + // and if they get destructed while being in use, that's bad. + std::map > info_; // Work queue and management PrioritizedWorkQueue *gameInfoWQ_; diff --git a/UI/GameScreen.cpp b/UI/GameScreen.cpp index f5710f9034eb..37729c24eda7 100644 --- a/UI/GameScreen.cpp +++ b/UI/GameScreen.cpp @@ -48,7 +48,7 @@ GameScreen::~GameScreen() { } void GameScreen::CreateViews() { - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); if (info && !info->id.empty()) saveDirs = info->GetSaveDataDirectories(); // Get's very heavy, let's not do it in update() @@ -154,7 +154,7 @@ UI::Choice *GameScreen::AddOtherChoice(UI::Choice *choice) { } UI::EventReturn GameScreen::OnCreateConfig(UI::EventParams &e) { - GameInfo *info = g_gameInfoCache->GetInfo(nullptr, gamePath_, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(nullptr, gamePath_, 0); if (!info) { return UI::EVENT_SKIPPED; } @@ -168,7 +168,7 @@ UI::EventReturn GameScreen::OnCreateConfig(UI::EventParams &e) { void GameScreen::CallbackDeleteConfig(bool yes) { if (yes) { - GameInfo *info = g_gameInfoCache->GetInfo(nullptr, gamePath_, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(nullptr, gamePath_, 0); if (!info) { return; } @@ -196,7 +196,7 @@ void GameScreen::update() { Draw::DrawContext *thin3d = screenManager()->getDrawContext(); - GameInfo *info = g_gameInfoCache->GetInfo(thin3d, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); + std::shared_ptr info = g_gameInfoCache->GetInfo(thin3d, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); if (tvTitle_) tvTitle_->SetText(info->GetTitle() + " (" + info->id + ")"); @@ -282,7 +282,7 @@ UI::EventReturn GameScreen::OnPlay(UI::EventParams &e) { } UI::EventReturn GameScreen::OnGameSettings(UI::EventParams &e) { - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); if (info && info->paramSFOLoaded) { std::string discID = info->paramSFO.GetValueString("DISC_ID"); screenManager()->push(new GameSettingsScreen(gamePath_, discID, true)); @@ -293,7 +293,7 @@ UI::EventReturn GameScreen::OnGameSettings(UI::EventParams &e) { UI::EventReturn GameScreen::OnDeleteSaveData(UI::EventParams &e) { I18NCategory *di = GetI18NCategory("Dialog"); I18NCategory *ga = GetI18NCategory("Game"); - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); if (info) { // Check that there's any savedata to delete if (saveDirs.size()) { @@ -308,7 +308,7 @@ UI::EventReturn GameScreen::OnDeleteSaveData(UI::EventParams &e) { } void GameScreen::CallbackDeleteSaveData(bool yes) { - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); if (yes) { info->DeleteAllSaveData(); info->saveDataSize = 0; @@ -319,7 +319,7 @@ void GameScreen::CallbackDeleteSaveData(bool yes) { UI::EventReturn GameScreen::OnDeleteGame(UI::EventParams &e) { I18NCategory *di = GetI18NCategory("Dialog"); I18NCategory *ga = GetI18NCategory("Game"); - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); if (info) { screenManager()->push( new PromptScreen(di->T("DeleteConfirmGame", "Do you really want to delete this game\nfrom your device? You can't undo this."), ga->T("ConfirmDelete"), di->T("Cancel"), @@ -330,7 +330,7 @@ UI::EventReturn GameScreen::OnDeleteGame(UI::EventParams &e) { } void GameScreen::CallbackDeleteGame(bool yes) { - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); if (yes) { info->Delete(); g_gameInfoCache->Clear(); @@ -339,7 +339,7 @@ void GameScreen::CallbackDeleteGame(bool yes) { } UI::EventReturn GameScreen::OnCreateShortcut(UI::EventParams &e) { - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); if (info) { host->CreateDesktopShortcut(gamePath_, info->GetTitle()); } @@ -414,7 +414,7 @@ void SetBackgroundPopupScreen::CreatePopupContents(UI::ViewGroup *parent) { void SetBackgroundPopupScreen::update() { PopupScreen::update(); - GameInfo *info = g_gameInfoCache->GetInfo(nullptr, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTBGDATA); + std::shared_ptr info = g_gameInfoCache->GetInfo(nullptr, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTBGDATA); if (status_ == Status::PENDING && info && !info->IsPending()) { GameInfoTex *pic = nullptr; if (info->pic1.dataLoaded && info->pic1.data.size()) { diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index 45b7d04e302b..97da89ddc333 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -175,7 +175,7 @@ class GameButton : public UI::Clickable { }; void GameButton::Draw(UIContext &dc) { - GameInfo *ginfo = g_gameInfoCache->GetInfo(dc.GetDrawContext(), gamePath_, 0); + std::shared_ptr ginfo = g_gameInfoCache->GetInfo(dc.GetDrawContext(), gamePath_, 0); Draw::Texture *texture = 0; u32 color = 0, shadowColor = 0; using namespace UI; @@ -1008,7 +1008,7 @@ void MainScreen::DrawBackground(UIContext &dc) { bool MainScreen::DrawBackgroundFor(UIContext &dc, const std::string &gamePath, float progress) { dc.Flush(); - GameInfo *ginfo = 0; + std::shared_ptr ginfo; if (!gamePath.empty()) { ginfo = g_gameInfoCache->GetInfo(dc.GetDrawContext(), gamePath, GAMEINFO_WANTBG); // Loading texture data may bind a texture. @@ -1042,8 +1042,7 @@ UI::EventReturn MainScreen::OnGameSelected(UI::EventParams &e) { #else std::string path = e.s; #endif - GameInfo *ginfo = 0; - ginfo = g_gameInfoCache->GetInfo(nullptr, path, GAMEINFO_WANTBG); + std::shared_ptr ginfo = g_gameInfoCache->GetInfo(nullptr, path, GAMEINFO_WANTBG); if (ginfo && ginfo->fileType == IdentifiedFileType::PSP_SAVEDATA_DIRECTORY) { return UI::EVENT_DONE; } diff --git a/UI/MiscScreens.cpp b/UI/MiscScreens.cpp index 3ab7bbf4e042..6d1a0d914853 100644 --- a/UI/MiscScreens.cpp +++ b/UI/MiscScreens.cpp @@ -69,7 +69,7 @@ static const uint32_t colors[4] = { 0xC0FFFFFF, }; -static ManagedTexture *bgTexture = nullptr; +static std::unique_ptr bgTexture; void UIBackgroundInit(UIContext &dc) { const std::string bgPng = GetSysDirectory(DIRECTORY_SYSTEM) + "background.png"; @@ -81,8 +81,7 @@ void UIBackgroundInit(UIContext &dc) { } void UIBackgroundShutdown() { - delete bgTexture; - bgTexture = nullptr; + bgTexture.reset(nullptr); } void DrawBackground(UIContext &dc, float alpha) { @@ -128,7 +127,7 @@ void DrawBackground(UIContext &dc, float alpha) { } void DrawGameBackground(UIContext &dc, const std::string &gamePath) { - GameInfo *ginfo = nullptr; + std::shared_ptr ginfo; if (gamePath.size()) ginfo = g_gameInfoCache->GetInfo(dc.GetDrawContext(), gamePath, GAMEINFO_WANTBG); dc.Flush(); diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index bbcd0980f9a6..717177ef81ff 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -120,7 +120,7 @@ static UI::Theme ui_theme; #include "android/android-ndk-profiler/prof.h" #endif -ManagedTexture *uiTexture; +std::unique_ptr uiTexture; ScreenManager *screenManager; std::string config_filename; @@ -681,8 +681,7 @@ void NativeShutdownGraphics() { UIBackgroundShutdown(); - delete uiTexture; - uiTexture = nullptr; + uiTexture.reset(nullptr); delete uiContext; uiContext = nullptr; diff --git a/UI/PauseScreen.cpp b/UI/PauseScreen.cpp index c1731ffc2969..f0630fb3dfa8 100644 --- a/UI/PauseScreen.cpp +++ b/UI/PauseScreen.cpp @@ -46,9 +46,7 @@ AsyncImageFileView::AsyncImageFileView(const std::string &filename, UI::ImageSizeMode sizeMode, PrioritizedWorkQueue *wq, UI::LayoutParams *layoutParams) : UI::Clickable(layoutParams), canFocus_(true), filename_(filename), color_(0xFFFFFFFF), sizeMode_(sizeMode), textureFailed_(false), fixedSizeW_(0.0f), fixedSizeH_(0.0f) {} -AsyncImageFileView::~AsyncImageFileView() { - delete texture_; -} +AsyncImageFileView::~AsyncImageFileView() {} void AsyncImageFileView::GetContentDimensions(const UIContext &dc, float &w, float &h) const { if (texture_) { @@ -75,16 +73,15 @@ void AsyncImageFileView::SetFilename(std::string filename) { if (filename_ != filename) { textureFailed_ = false; filename_ = filename; - delete texture_; - texture_ = nullptr; + texture_.reset(nullptr); } } void AsyncImageFileView::Draw(UIContext &dc) { using namespace Draw; if (!texture_ && !textureFailed_ && !filename_.empty()) { - texture_ = CreateTextureFromFile(dc.GetDrawContext(), filename_.c_str(), DETECT, true); - if (!texture_) + texture_ = std::move(CreateTextureFromFile(dc.GetDrawContext(), filename_.c_str(), DETECT, true)); + if (!texture_.get()) textureFailed_ = true; } @@ -414,7 +411,7 @@ UI::EventReturn GamePauseScreen::OnSwitchUMD(UI::EventParams &e) { void GamePauseScreen::CallbackDeleteConfig(bool yes) { if (yes) { - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); g_Config.unloadGameConfig(); g_Config.deleteGameConfig(info->id); info->hasConfig = false; @@ -428,7 +425,7 @@ UI::EventReturn GamePauseScreen::OnCreateConfig(UI::EventParams &e) g_Config.createGameConfig(gameId); g_Config.changeGameSpecific(gameId); g_Config.saveGameConfig(gameId); - GameInfo *info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); + std::shared_ptr info = g_gameInfoCache->GetInfo(NULL, gamePath_, 0); if (info) { info->hasConfig = true; } diff --git a/UI/PauseScreen.h b/UI/PauseScreen.h index af128b21973c..44c52f6d4543 100644 --- a/UI/PauseScreen.h +++ b/UI/PauseScreen.h @@ -18,6 +18,7 @@ #pragma once #include +#include #include "ui/ui_screen.h" #include "ui/viewgroup.h" @@ -91,7 +92,7 @@ class AsyncImageFileView : public UI::Clickable { uint32_t color_; UI::ImageSizeMode sizeMode_; - ManagedTexture *texture_ = nullptr; + std::unique_ptr texture_; bool textureFailed_; float fixedSizeW_; float fixedSizeH_; diff --git a/UI/SavedataScreen.cpp b/UI/SavedataScreen.cpp index 9c998163976e..1bc67539345d 100644 --- a/UI/SavedataScreen.cpp +++ b/UI/SavedataScreen.cpp @@ -70,7 +70,7 @@ class SavedataPopupScreen : public PopupScreen { UIContext &dc = *screenManager()->getUIContext(); const Style &textStyle = dc.theme->popupStyle; - GameInfo *ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); + std::shared_ptr ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savePath_, GAMEINFO_WANTBG | GAMEINFO_WANTSIZE); LinearLayout *content = new LinearLayout(ORIENT_VERTICAL); parent->Add(content); if (!ginfo) @@ -143,7 +143,7 @@ class SavedataButton : public UI::Clickable { }; UI::EventReturn SavedataPopupScreen::OnDeleteButtonClick(UI::EventParams &e) { - GameInfo *ginfo = g_gameInfoCache->GetInfo(nullptr, savePath_, GAMEINFO_WANTSIZE); + std::shared_ptr ginfo = g_gameInfoCache->GetInfo(nullptr, savePath_, GAMEINFO_WANTSIZE); ginfo->Delete(); TriggerFinish(DR_NO); return UI::EVENT_DONE; @@ -157,7 +157,7 @@ static std::string CleanSaveString(std::string str) { } void SavedataButton::Draw(UIContext &dc) { - GameInfo *ginfo = g_gameInfoCache->GetInfo(dc.GetDrawContext(), savePath_, GAMEINFO_WANTSIZE); + std::shared_ptr ginfo = g_gameInfoCache->GetInfo(dc.GetDrawContext(), savePath_, GAMEINFO_WANTSIZE); Draw::Texture *texture = 0; u32 color = 0, shadowColor = 0; using namespace UI; @@ -381,7 +381,7 @@ void SavedataScreen::CreateViews() { } UI::EventReturn SavedataScreen::OnSavedataButtonClick(UI::EventParams &e) { - GameInfo *ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), e.s, 0); + std::shared_ptr ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), e.s, 0); SavedataPopupScreen *popupScreen = new SavedataPopupScreen(e.s, ginfo->GetTitle()); if (e.v) { popupScreen->SetPopupOrigin(e.v); diff --git a/UI/Store.cpp b/UI/Store.cpp index 1aa35fad364d..5ef5be89860a 100644 --- a/UI/Store.cpp +++ b/UI/Store.cpp @@ -54,12 +54,11 @@ std::string ResolveUrl(std::string baseUrl, std::string url) { class HttpImageFileView : public UI::View { public: HttpImageFileView(http::Downloader *downloader, const std::string &path, UI::ImageSizeMode sizeMode = UI::IS_DEFAULT, UI::LayoutParams *layoutParams = 0) - : UI::View(layoutParams), path_(path), color_(0xFFFFFFFF), sizeMode_(sizeMode), downloader_(downloader), texture_(nullptr), textureFailed_(false), fixedSizeW_(0.0f), fixedSizeH_(0.0f) {} + : UI::View(layoutParams), path_(path), sizeMode_(sizeMode), downloader_(downloader) {} ~HttpImageFileView() { if (download_) download_->Cancel(); - delete texture_; } void GetContentDimensions(const UIContext &dc, float &w, float &h) const override; @@ -77,18 +76,18 @@ class HttpImageFileView : public UI::View { private: void DownloadCompletedCallback(http::Download &download); - bool canFocus_; + bool canFocus_ = false; std::string path_; - uint32_t color_; + uint32_t color_ = 0xFFFFFFFF; UI::ImageSizeMode sizeMode_; http::Downloader *downloader_; std::shared_ptr download_; std::string textureData_; - ManagedTexture *texture_; - bool textureFailed_; - float fixedSizeW_; - float fixedSizeH_; + std::unique_ptr texture_; + bool textureFailed_ = false; + float fixedSizeW_ = 0.0f; + float fixedSizeH_ = 0.0f; }; void HttpImageFileView::GetContentDimensions(const UIContext &dc, float &w, float &h) const { @@ -116,10 +115,7 @@ void HttpImageFileView::SetFilename(std::string filename) { if (path_ != filename) { textureFailed_ = false; path_ = filename; - if (texture_) { - delete texture_; - texture_ = nullptr; - } + texture_.reset(nullptr); } } diff --git a/UI/TextureUtil.cpp b/UI/TextureUtil.cpp index 927836994b39..6ee742751b30 100644 --- a/UI/TextureUtil.cpp +++ b/UI/TextureUtil.cpp @@ -153,23 +153,23 @@ bool ManagedTexture::LoadFromFile(const std::string &filename, ImageFileType typ return retval; } -ManagedTexture *CreateTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType type, bool generateMips) { +std::unique_ptr CreateTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType type, bool generateMips) { if (!draw) - return nullptr; + return std::unique_ptr(); // TODO: Load the texture on a background thread. ManagedTexture *mtex = new ManagedTexture(draw); if (!mtex->LoadFromFile(filename, type, generateMips)) { delete mtex; - return nullptr; + return std::unique_ptr(); } - return mtex; + return std::move(std::unique_ptr(mtex)); } // TODO: Remove the code duplication between this and LoadFromFileData -ManagedTexture *CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType type, bool generateMips) { +std::unique_ptr CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType type, bool generateMips) { if (!draw) - return nullptr; + return std::unique_ptr(); ManagedTexture *mtex = new ManagedTexture(draw); mtex->LoadFromFileData(data, size, type, generateMips); - return mtex; + return std::move(std::unique_ptr(mtex)); } \ No newline at end of file diff --git a/UI/TextureUtil.h b/UI/TextureUtil.h index 71b87feb5a65..ce512d76abe8 100644 --- a/UI/TextureUtil.h +++ b/UI/TextureUtil.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "thin3d/thin3d.h" #include "Core/Config.h" #include "gfx/gl_lost_manager.h" @@ -55,5 +57,5 @@ class ManagedTexture : public GfxResourceHolder { std::string filename_; // Textures that are loaded from files can reload themselves automatically. }; -ManagedTexture *CreateTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType fileType, bool generateMips = false); -ManagedTexture *CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType fileType, bool generateMips = false); +std::unique_ptr CreateTextureFromFile(Draw::DrawContext *draw, const char *filename, ImageFileType fileType, bool generateMips = false); +std::unique_ptr CreateTextureFromFileData(Draw::DrawContext *draw, const uint8_t *data, int size, ImageFileType fileType, bool generateMips = false); diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 3e5d53c41a6b..806e4288c998 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -1068,7 +1068,7 @@ extern "C" jstring Java_org_ppsspp_ppsspp_ShortcutActivity_queryGameName(JNIEnv std::string result = ""; GameInfoCache *cache = new GameInfoCache(); - GameInfo *info = cache->GetInfo(nullptr, path, 0); + std::shared_ptr info = cache->GetInfo(nullptr, path, 0); // Wait until it's done: this is synchronous, unfortunately. if (info) { cache->WaitUntilDone(info);