From bc751c18a7ac258b89e1bd251bf349230bb953f2 Mon Sep 17 00:00:00 2001 From: d87 Date: Sat, 14 Nov 2020 22:00:33 +0700 Subject: [PATCH] Switched ImageContainer to shared_ptr --- src/D4See.cpp | 10 +++++----- src/WindowManager.cpp | 22 +++++++++------------- src/WindowManager.h | 8 ++++---- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/D4See.cpp b/src/D4See.cpp index 11d06f2..1d422b1 100644 --- a/src/D4See.cpp +++ b/src/D4See.cpp @@ -35,7 +35,7 @@ VOID OnPaint() //HDC hdc) { using namespace std::chrono_literals; - ImageContainer* frame = gWinMgr.frame; + std::shared_ptr& frame = gWinMgr.frame; if (frame) { @@ -357,7 +357,7 @@ INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpCmdLine, INT iCmdSho ); hr = pD2DFactory->CreateHwndRenderTarget(props, D2D1::HwndRenderTargetProperties(hWnd, size), &pRenderTarget); - gWinMgr.SelectImage(new ImageContainer(hWnd, playlist->Current()->path, playlist->Current()->format)); + gWinMgr.SelectImage(std::make_shared(hWnd, playlist->Current()->path, playlist->Current()->format)); gWinMgr.frame->threadInitFinished.wait(); gWinMgr.ResizeForImage(); @@ -400,7 +400,7 @@ INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpCmdLine, INT iCmdSho case WM_FRAMEERROR: case WM_FRAMEREADY: { ImageContainer* f = (ImageContainer*)msg.wParam; - if (f == gWinMgr.frame) { + if (f == gWinMgr.frame.get()) { //gWinMgr.newImagePending = true; //RedrawWindow(hWnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE); LOG("Accepted WM_FRAMEREADY for {0}", wide_to_utf8(f->filename)); @@ -427,7 +427,7 @@ INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpCmdLine, INT iCmdSho gWinMgr.SelectPlaylist(playlist); auto cur = playlist->Current(); - gWinMgr.SelectImage(new ImageContainer(hWnd, cur->path, cur->format)); + gWinMgr.SelectImage(std::make_shared(hWnd, cur->path, cur->format)); break; } @@ -444,7 +444,7 @@ INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR lpCmdLine, INT iCmdSho auto delta = now - prevTime; if (gWinMgr.frame) { - ImageContainer* frame = gWinMgr.frame; + std::shared_ptr& frame = gWinMgr.frame; if (!frame->isAnimated) { if (frame->decoderBatchId != frame->drawId) { LOG("Batch Ids: {0} - {1}", frame->drawId, frame->decoderBatchId); diff --git a/src/WindowManager.cpp b/src/WindowManager.cpp index 748532a..561e6d0 100644 --- a/src/WindowManager.cpp +++ b/src/WindowManager.cpp @@ -13,8 +13,8 @@ void WindowManager::StopTimer(UINT_PTR id) { WindowManager::~WindowManager() { if (playlist) delete playlist; - if (frame) delete frame; - if (frame_prefetch) delete frame_prefetch; + //if (frame) delete frame; + //if (frame_prefetch) delete frame_prefetch; } //void ClearWindowForFrame(HWND hWnd, ImageContainer* f) { @@ -53,12 +53,12 @@ void WindowManager::ShowPrefetch() { } void WindowManager::DiscardPrefetch() { - if (frame_prefetch) - delete frame_prefetch; + //if (frame_prefetch) + //delete frame_prefetch; frame_prefetch = nullptr; } -void WindowManager::StartPrefetch(ImageContainer* f) { +void WindowManager::StartPrefetch(std::shared_ptr f) { frame_prefetch = f; // Start 2 minute timer SetTimer(hWnd, D4S_PREFETCH_TIMEOUT, 120000, NULL); @@ -94,11 +94,11 @@ void WindowManager::LoadImageFromPlaylist(int prefetchDir) { } else { // Changed direction or jumped more than 1 DiscardPrefetch(); - SelectImage(new ImageContainer(hWnd, cur->path, cur->format)); + SelectImage(std::make_shared(hWnd, cur->path, cur->format)); } } if (following) { - StartPrefetch(new ImageContainer(hWnd, following->path, following->format)); + StartPrefetch(std::make_shared(hWnd, following->path, following->format)); } else { frame_prefetch = nullptr; @@ -245,11 +245,7 @@ void WindowManager::ToggleBorderless(int doRedraw) { } } -void WindowManager::SelectImage(ImageContainer* f) { - if (frame != nullptr) { - delete frame; - } - +void WindowManager::SelectImage(std::shared_ptr f) { LOG_DEBUG("<<<<<<< FRAME SWITCH >>>>>>>"); //LOG_DEBUG(" -- {0} --", f->filename); frame = f; @@ -662,7 +658,7 @@ void WindowManager::HandleMenuCommand(unsigned int uIDItem) { if (filepath != L"") { auto pl = new Playlist(filepath); SelectPlaylist(pl); - SelectImage(new ImageContainer(hWnd, pl->Current()->path, pl->Current()->format)); + SelectImage(std::make_shared(hWnd, pl->Current()->path, pl->Current()->format)); //std::cout << filepath.c_str() << std::endl; } break; diff --git a/src/WindowManager.h b/src/WindowManager.h index e89f515..36876d0 100644 --- a/src/WindowManager.h +++ b/src/WindowManager.h @@ -34,8 +34,8 @@ struct WINDOW_SAVED_DATA { class WindowManager { public: HWND hWnd; - ImageContainer* frame = nullptr; - ImageContainer* frame_prefetch = nullptr; // prefecth container + std::shared_ptr frame = nullptr; + std::shared_ptr frame_prefetch = nullptr; // prefecth container Playlist* playlist = nullptr; PlaylistSortMethod sortMethod = PlaylistSortMethod::ByName; D4See::InputHandler input; @@ -81,11 +81,11 @@ class WindowManager { void NextImage(); void PreviousImage(); void LoadImageFromPlaylist(int prefetchDir); - void StartPrefetch(ImageContainer* f); + void StartPrefetch(std::shared_ptr f); void ShowPrefetch(); void LimitPanOffset(); void DiscardPrefetch(); - void SelectImage(ImageContainer* f); + void SelectImage(std::shared_ptr f); void SelectPlaylist(Playlist* playlist); void GetWindowSizeForImage(RECT& rrc); void ResizeForImage();