From cd4510c77d3fdb514c2b33570da7bc97670e16d7 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 16 Jul 2020 08:58:52 +0300
Subject: [PATCH] Fix for GLTexture memory leak

---
 applications/gui2/src/main.cpp                       |  5 +++--
 applications/gui2/src/widgets/imageview.cpp          |  3 +++
 applications/gui2/src/widgets/imageview.hpp          |  6 +++++-
 components/renderers/cpp/src/gltexture.cpp           |  2 +-
 components/structures/include/ftl/data/new_frame.hpp |  2 +-
 components/structures/src/new_frame.cpp              | 12 +++++++++++-
 6 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/applications/gui2/src/main.cpp b/applications/gui2/src/main.cpp
index e704df5c7..ead435858 100644
--- a/applications/gui2/src/main.cpp
+++ b/applications/gui2/src/main.cpp
@@ -182,10 +182,11 @@ int main(int argc, char **argv) {
 			#endif
 			return -1;
 		}
-
-		nanogui::shutdown();
 	}
 
+	// Must be after ~FTLGui since it destroys GL context.
+	nanogui::shutdown();
+
 	// Save config changes and delete final objects
 	ftl::config::cleanup();
 
diff --git a/applications/gui2/src/widgets/imageview.cpp b/applications/gui2/src/widgets/imageview.cpp
index 3ef6f5c69..791176346 100644
--- a/applications/gui2/src/widgets/imageview.cpp
+++ b/applications/gui2/src/widgets/imageview.cpp
@@ -466,6 +466,9 @@ void ftl::gui2::ImageView::writePixelInfo(NVGcontext* ctx, const Vector2f& cellP
 
 ////////////////////////////////////////////////////////////////////////////////
 
+FTLImageView::~FTLImageView() {
+}
+
 void FTLImageView::draw(NVGcontext* ctx) {
 	if (texture_.isValid()) { ImageView::draw(ctx); }
 }
diff --git a/applications/gui2/src/widgets/imageview.hpp b/applications/gui2/src/widgets/imageview.hpp
index c54a0f531..94d3d59c9 100644
--- a/applications/gui2/src/widgets/imageview.hpp
+++ b/applications/gui2/src/widgets/imageview.hpp
@@ -35,7 +35,7 @@ namespace gui2 {
 class NANOGUI_EXPORT ImageView : public nanogui::Widget {
 public:
 	ImageView(nanogui::Widget* parent, GLuint imageID = -1);
-	~ImageView();
+	virtual ~ImageView();
 
 	void bindImage(GLuint imageId);
 
@@ -187,6 +187,10 @@ public:
 class FTLImageView : public ImageView {
 public:
 	using ImageView::ImageView;
+
+	FTLImageView(nanogui::Widget* parent, GLuint imageID = -1) : ImageView(parent, imageID) {}
+	~FTLImageView();
+
 	virtual void draw(NVGcontext* ctx) override;
 	virtual nanogui::Vector2i preferredSize(NVGcontext* ctx) const override;
 
diff --git a/components/renderers/cpp/src/gltexture.cpp b/components/renderers/cpp/src/gltexture.cpp
index 324739f55..2bb106ecd 100644
--- a/components/renderers/cpp/src/gltexture.cpp
+++ b/components/renderers/cpp/src/gltexture.cpp
@@ -27,7 +27,7 @@ GLTexture::GLTexture() {
 }
 
 GLTexture::~GLTexture() {
-	//glDeleteTextures(1, &glid_);
+	free();  // Note: Do not simply remove this...
 }
 
 void GLTexture::make(int width, int height, Type type) {
diff --git a/components/structures/include/ftl/data/new_frame.hpp b/components/structures/include/ftl/data/new_frame.hpp
index 7d779a68f..1533e034a 100644
--- a/components/structures/include/ftl/data/new_frame.hpp
+++ b/components/structures/include/ftl/data/new_frame.hpp
@@ -172,7 +172,7 @@ class Frame {
 
 	protected:
 	// Only Feed class should construct
-	Frame(Pool *ppool, Session *parent, FrameID pid, int64_t ts) : timestamp_(ts), id_(pid), pool_(ppool), parent_(parent), status_(FrameStatus::CREATED) {};
+	Frame(Pool *ppool, Session *parent, FrameID pid, int64_t ts);
 	int64_t timestamp_=0;
 	FrameID id_;
 
diff --git a/components/structures/src/new_frame.cpp b/components/structures/src/new_frame.cpp
index 88619e19e..64f64846b 100644
--- a/components/structures/src/new_frame.cpp
+++ b/components/structures/src/new_frame.cpp
@@ -67,10 +67,20 @@ void ftl::data::setTypeEncoder(size_t type, const std::function<bool(const ftl::
 
 //==============================================================================
 
+//static std::atomic_int frame_count = 0;
+
+Frame::Frame(Pool *ppool, Session *parent, FrameID pid, int64_t ts)
+ : timestamp_(ts), id_(pid), pool_(ppool), parent_(parent), status_(FrameStatus::CREATED) {
+	//LOG(INFO) << "Frames: " << ++frame_count;
+ };
+
 Frame::~Frame() {
 	if (status_ == FrameStatus::CREATED) store();
 	if (status_ == FrameStatus::STORED) flush();
-	if (status_ != FrameStatus::RELEASED && pool_) pool_->release(*this);
+	if (status_ != FrameStatus::RELEASED && pool_) {
+		pool_->release(*this);
+		//--frame_count;
+	}
 };
 
 bool ftl::data::Frame::hasAll(const std::unordered_set<ftl::codecs::Channel> &cs) {
-- 
GitLab