From d43682ab27441ae02509de669961854fab0980c6 Mon Sep 17 00:00:00 2001 From: Parth Date: Fri, 7 Aug 2020 14:45:21 -0700 Subject: [PATCH] Fixed memory leaks --- CGUI.hh | 3 +- Converter/Convert.hh | 2 +- Custom/DeleteOnQuit/DeleteOnQuit.hh | 55 ++++++++++ Custom/Initialize/Initialize.hh | 11 +- Custom/Storage/Storage.hh | 151 ---------------------------- Widgets/Container.hh | 10 +- Widgets/EventBox/EventBox.cpp | 37 +++++++ Widgets/EventBox/EventBox.hh | 19 ++++ Widgets/Handler.hh | 55 +++++----- 9 files changed, 147 insertions(+), 196 deletions(-) create mode 100644 Custom/DeleteOnQuit/DeleteOnQuit.hh delete mode 100644 Custom/Storage/Storage.hh create mode 100644 Widgets/EventBox/EventBox.cpp create mode 100644 Widgets/EventBox/EventBox.hh diff --git a/CGUI.hh b/CGUI.hh index 728a2d2..9cefe8c 100644 --- a/CGUI.hh +++ b/CGUI.hh @@ -18,7 +18,7 @@ #include "./Widgets/Dialog/Dialog.hh" #include "./Widgets/FileChooser/FileChooser.hh" #include "./Custom/List/List.hh" -#include "./Custom/Storage/Storage.hh" +#include "./Custom/DeleteOnQuit/DeleteOnQuit.hh" #include "./Widgets/FileChooserDialog/FileChooserDialog.hh" #include "./Widgets/StyleContext.hh" #include "./Widgets/Grid/Grid.hh" @@ -57,6 +57,7 @@ #include "./Widgets/SpinButton/SpinButton.hh" #include "./Widgets/ListBox/ListBox.hh" #include "./Widgets/FlowBox/FlowBox.hh" +#include "./Widgets/EventBox/EventBox.hh" #include "./Pixbuf/Pixbuf.hh" #include "./Custom/Initialize/Initialize.hh" #include "./Widgets/Entry/EntryBuffer/EntryBuffer.hh" diff --git a/Converter/Convert.hh b/Converter/Convert.hh index 93bcb8c..5b55c2e 100644 --- a/Converter/Convert.hh +++ b/Converter/Convert.hh @@ -140,7 +140,7 @@ namespace CGui enum class SizeRequestMode { HEIGHT_FOR_WIDTH, WIDTH_FOR_HEIGHT, CONSTANT_SIZE }; enum class Gravity { NORTH_WEST = 1, NORTH, NORTH_EAST, WEST, CENTER, EAST, SOUTH_WEST, SOUTH, SOUTH_EAST, STATIC }; - + struct WindowPositionData { int root_x; diff --git a/Custom/DeleteOnQuit/DeleteOnQuit.hh b/Custom/DeleteOnQuit/DeleteOnQuit.hh new file mode 100644 index 0000000..9436c9c --- /dev/null +++ b/Custom/DeleteOnQuit/DeleteOnQuit.hh @@ -0,0 +1,55 @@ +#pragma once + +#include +#include "../List/List.hh" + +namespace CGui +{ + class DeleteOnQuit + { + public: + DeleteOnQuit(const DeleteOnQuit&) = delete; + DeleteOnQuit(const DeleteOnQuit&&) = delete; + DeleteOnQuit& operator=(const DeleteOnQuit&) = delete; + DeleteOnQuit& operator=(const DeleteOnQuit&&) = delete; + DeleteOnQuit& operator==(const DeleteOnQuit&) = delete; + DeleteOnQuit& operator==(const DeleteOnQuit&&) = delete; + + static DeleteOnQuit& GetInstance() + { + static DeleteOnQuit instance; + return instance; + } + + void Add(void* data) + { + l_instance->Insert(data); + } + + void ForEach(void(*func)(void*)) + { + l_instance->ForEach(func); + } + + Single::List* list() + { + return l_instance; + } + + ~DeleteOnQuit() + { + l_instance->ForEach([](void* data) -> void + { + delete data; + }); + delete l_instance; + } + + private: + DeleteOnQuit() : l_instance{ new Single::List } + { + } + + Single::List* l_instance; + }; +}; diff --git a/Custom/Initialize/Initialize.hh b/Custom/Initialize/Initialize.hh index f34aab1..50f7bdc 100644 --- a/Custom/Initialize/Initialize.hh +++ b/Custom/Initialize/Initialize.hh @@ -1,6 +1,6 @@ #pragma once #include -#include "../Storage/Storage.hh" +#include "../DeleteOnQuit/DeleteOnQuit.hh" namespace CGui { @@ -66,15 +66,6 @@ namespace CGui Storage::GetInstance().ForEach*>(f4, "mainlistsignals"); Storage::GetInstance().Free*>("mainlistsignals");*/ - auto free = [](KeyValue::Node * node) -> void - { - if (std::string(node->key) == "Instance") - delete node->value; - }; - - Storage::GetInstance().ForEach(free, "deleteonquit"); - Storage::GetInstance().Free("deleteonquit"); - gtk_main_quit(); } }; \ No newline at end of file diff --git a/Custom/Storage/Storage.hh b/Custom/Storage/Storage.hh deleted file mode 100644 index eebcc5a..0000000 --- a/Custom/Storage/Storage.hh +++ /dev/null @@ -1,151 +0,0 @@ -#pragma once - -#include -#include "../List/List.hh" -#include - -namespace CGui -{ - class Storage - { - public: - Storage(const Storage&) = delete; - Storage(const Storage&&) = delete; - Storage& operator=(const Storage&) = delete; - Storage& operator==(const Storage&) = delete; - - ~Storage() - { - delete std::any_cast*>(l_instance->First().value); - delete l_instance; - } - - template void Add(std::any key, std::any value, const char* passkey = NULL) - { - if (passkey == NULL) - { - auto innerList = std::any_cast*>(l_instance->First().value); - innerList->Insert(key, value); - } - else - { - auto f = [](KeyValue::Node * node, const char* pass, std::any * key, std::any * value) - { - try - { - if (std::any_cast(node->key) != "NULL" && std::any_cast(node->key) == std::string(pass)) - { - auto innerList = std::any_cast*>(node->value); - innerList->Insert(std::any_cast(*key), std::any_cast(*value)); - } - } - catch (const std::bad_any_cast & e) - { - std::cout << e.what() << std::endl; - } - }; - - l_instance->ForEach((void(*)(KeyValue::Node*, const char*, std::any*, std::any*))f, passkey, &key, &value); - } - } - - template void ForEach(void(*func)(KeyValue::Node*), const char* passkey = NULL) - { - if (passkey == NULL || std::string(passkey) == "NULL") - { - auto innerList = std::any_cast*>(l_instance->First().value); - innerList->ForEach(func); - } - else - { - auto f = [](KeyValue::Node * node, const char* pass, void(*func)(KeyValue::Node*)) - { - try - { - if (std::any_cast(node->key) != "NULL" && std::any_cast(node->key) == std::string(pass)) - { - auto innerList = std::any_cast*>(node->value); - innerList->ForEach(func); - } - } - catch (const std::bad_any_cast & e) - { - std::cout << e.what() << std::endl; - } - }; - - l_instance->ForEach((void(*)(KeyValue::Node*, const char*, void(*)(KeyValue::Node*)))f, passkey, func); - } - } - - static Storage& GetInstance() - { - static Storage instance; - return instance; - } - - bool IsExistsPrivate(const char* passkey) - { - bool exists = false; - auto f = [](KeyValue::Node * node, bool* e, const char* p) -> void - { - if (std::any_cast(node->key) == std::string(p)) - * e = true; - }; - - l_instance->ForEach((void(*)(KeyValue::Node*, bool*, const char*))f, &exists, passkey); - - return exists; - } - - template bool MakePrivate(const char* passkey) - { - if (!GetInstance().IsExistsPrivate(passkey)) - { - bool inserted = false; - auto f = [](KeyValue::Node * node, bool* i, const char* pass, KeyValue::List * l) - { - if (!*i) - if (std::string(pass) == std::any_cast(node->key)) - * i = false; - else - { - l->Insert(std::string(pass), new KeyValue::List); - *i = true; - } - }; - l_instance->ForEach((void(*)(KeyValue::Node*, bool*, const char*, KeyValue::List*))f, &inserted, passkey, l_instance); - - return inserted; - } - else - return false; - } - - template bool Free(const char* passkey) - { - bool deleted; - auto f = +[](KeyValue::Node * node, const char* pass, bool* d) - { - if (std::string(pass) == std::any_cast(node->key)) - { - delete std::any_cast*>(node->value); - *d = true; - } - else - *d = false; - }; - - l_instance->ForEach((void(*)(KeyValue::Node*, const char*, bool*))f, passkey, &deleted); - - return deleted; - } - - private: - KeyValue::List* l_instance; - Storage() : l_instance{ new KeyValue::List } - { - l_instance->Insert(std::string("NULL"), new KeyValue::List); - } - }; -}; diff --git a/Widgets/Container.hh b/Widgets/Container.hh index 2cb0996..79ef755 100644 --- a/Widgets/Container.hh +++ b/Widgets/Container.hh @@ -3,7 +3,7 @@ #include #include "./Widget.hh" #include "../Custom/List/List.hh" -#include "../Custom/Storage/Storage.hh" +#include "../Custom/DeleteOnQuit/DeleteOnQuit.hh" namespace CGui { @@ -14,7 +14,7 @@ namespace CGui Container(WidgetType* w) { t_widget = w; - Storage::GetInstance().MakePrivate("deleteonquit"); + /*Storage::GetInstance().MakePrivate("deleteonquit");*/ } virtual ~Container() @@ -23,7 +23,7 @@ namespace CGui { children.ForEach([](void* data) { - Storage::GetInstance().Add("Instance", data, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(data); }); } } @@ -96,7 +96,6 @@ namespace CGui Container(GtkWidget* w) { t_widget = w; - Storage::GetInstance().MakePrivate("deleteonquit"); } virtual ~Container() @@ -105,7 +104,7 @@ namespace CGui { children.ForEach([](void* data) { - Storage::GetInstance().Add("Instance", data, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(data); }); } } @@ -167,7 +166,6 @@ namespace CGui protected: Container() { - Storage::GetInstance().MakePrivate("deleteonquit"); } GtkWidget* t_widget; diff --git a/Widgets/EventBox/EventBox.cpp b/Widgets/EventBox/EventBox.cpp new file mode 100644 index 0000000..ac11b19 --- /dev/null +++ b/Widgets/EventBox/EventBox.cpp @@ -0,0 +1,37 @@ +#include "./EventBox.hh" + +namespace CGui +{ + EventBox::EventBox() : Container(this), Handler(this) + { + widget = gtk_event_box_new(); + this->SetContext(widget); + } + + EventBox::EventBox(Widget& w) : Container(this), Handler(this) + { + widget = gtk_event_box_new(); + this->SetContext(widget); + this->Add(w); + } + + void EventBox::VisibleWindow(bool visible) + { + gtk_event_box_set_visible_window(GTK_EVENT_BOX(widget), visible); + } + + bool EventBox::VisibleWindow() + { + return gtk_event_box_get_visible_window(GTK_EVENT_BOX(widget)); + } + + void EventBox::AboveChild(bool above) + { + gtk_event_box_set_above_child(GTK_EVENT_BOX(widget), above); + } + + bool EventBox::AboveChild() + { + return gtk_event_box_get_above_child(GTK_EVENT_BOX(widget)); + } +} \ No newline at end of file diff --git a/Widgets/EventBox/EventBox.hh b/Widgets/EventBox/EventBox.hh new file mode 100644 index 0000000..7ffa79a --- /dev/null +++ b/Widgets/EventBox/EventBox.hh @@ -0,0 +1,19 @@ +#pragma once + +#include "../Widget.hh" +#include "../Container.hh" +#include "../Handler.hh" + +namespace CGui +{ + class EventBox : public Widget, public Container, public Handler + { + public: + EventBox(); + EventBox(Widget& w); + void VisibleWindow(bool visible); + bool VisibleWindow(); + void AboveChild(bool above); + bool AboveChild(); + }; +} \ No newline at end of file diff --git a/Widgets/Handler.hh b/Widgets/Handler.hh index b1f7904..9ef3e56 100644 --- a/Widgets/Handler.hh +++ b/Widgets/Handler.hh @@ -1,9 +1,10 @@ #pragma once #include "../Converter/Convert.hh" -#include "../Custom/Storage/Storage.hh" +#include "../Custom/DeleteOnQuit/DeleteOnQuit.hh" #include "../Custom/List/List.hh" #include +#include namespace CGui { @@ -17,7 +18,7 @@ namespace CGui Storage::GetInstance().MakePrivate("passingdata"); Storage::GetInstance().MakePrivate*>("mainlistevents"); Storage::GetInstance().MakePrivate*>("mainlistsignals");*/ - Storage::GetInstance().MakePrivate("deleteonquit"); + //Storage::GetInstance().MakePrivate("deleteonquit"); t_widget = w; } @@ -65,9 +66,9 @@ namespace CGui { func, t_widget }; - Storage::GetInstance().Add("Instance", pass, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(pass); - auto callback = [](GtkWidget * widget, PassingDataByFunc * func_data) + auto callback = [](GtkWidget* widget, PassingDataByFunc* func_data) { auto user_func = std::any_cast(func_data->func); user_func(); @@ -120,9 +121,9 @@ namespace CGui { func, t_widget }; - Storage::GetInstance().Add("Instance", pass, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(pass); - auto callback = [](GtkWidget * widget, PassingDataByFunc * func_data) + auto callback = [](GtkWidget* widget, PassingDataByFunc* func_data) { auto user_func = std::any_cast(func_data->func); user_func(func_data->ins); @@ -179,16 +180,16 @@ namespace CGui { std::make_tuple(func, &args...), t_widget }; - Storage::GetInstance().Add("Instance", pass, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(pass); - auto callback = [](GtkWidget * widget, PassingDataByFunc * func_data) + auto callback = [](GtkWidget* widget, PassingDataByFunc* func_data) { - auto apply_f = [&func_data](void(*user_func)(WidgetType*, Args * ...), Args * ... user_data) + auto apply_f = [&func_data](void(*user_func)(WidgetType*, Args* ...), Args* ... user_data) { user_func(func_data->ins, user_data...); }; - std::apply(apply_f, std::any_cast>(func_data->func)); + std::apply(apply_f, std::any_cast>(func_data->func)); }; return static_cast(g_signal_connect(G_OBJECT(t_widget->GetWidget()), Converter::Convert::GetInstance().GetGtkCode(signal), G_CALLBACK((void(*)(GtkWidget*, PassingDataByFunc*))callback), pass)); @@ -237,21 +238,21 @@ namespace CGui std::any_cast*>(keyvaluedata.value)->ForEach(f); };*/ - + PassingDataByFunc* pass = new PassingDataByFunc { std::make_tuple(func, &args...), t_widget }; - Storage::GetInstance().Add("Instance", pass, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(pass); - auto callback = [](GtkWidget * widget, PassingDataByFunc * func_data) + auto callback = [](GtkWidget* widget, PassingDataByFunc* func_data) { - auto apply_f = [](void(*user_func)(Args * ...), Args * ... user_data) + auto apply_f = [](void(*user_func)(Args* ...), Args* ... user_data) { user_func(user_data...); }; - std::apply(apply_f, std::any_cast>(func_data->func)); + std::apply(apply_f, std::any_cast>(func_data->func)); }; return static_cast(g_signal_connect(G_OBJECT(t_widget->GetWidget()), Converter::Convert::GetInstance().GetGtkCode(signal), G_CALLBACK((void(*)(GtkWidget*, PassingDataByFunc*))callback), pass)); @@ -301,9 +302,9 @@ namespace CGui { func, t_widget }; - Storage::GetInstance().Add("Instance", pass, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(pass); - auto callback = [](GtkWidget * widget, GdkEvent *user_event, PassingDataByFunc * func_data) + auto callback = [](GtkWidget* widget, GdkEvent* user_event, PassingDataByFunc* func_data) { auto user_func = std::any_cast(func_data->func); user_func(); @@ -356,9 +357,9 @@ namespace CGui { func, t_widget }; - Storage::GetInstance().Add("Instance", pass, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(pass); - auto callback = [](GtkWidget * widget, GdkEvent * user_event, PassingDataByFunc * func_data) + auto callback = [](GtkWidget* widget, GdkEvent* user_event, PassingDataByFunc* func_data) { auto user_func = std::any_cast(func_data->func); user_func(func_data->ins); @@ -415,16 +416,16 @@ namespace CGui { std::make_tuple(func, &args...), t_widget }; - Storage::GetInstance().Add("Instance", pass, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(pass); - auto callback = [](GtkWidget * widget, GdkEvent * user_event, PassingDataByFunc * func_data) + auto callback = [](GtkWidget* widget, GdkEvent* user_event, PassingDataByFunc* func_data) { - auto apply_f = [&func_data](void(*user_func)(WidgetType*, Args * ...), Args * ... user_data) + auto apply_f = [&func_data](void(*user_func)(WidgetType*, Args* ...), Args* ... user_data) { user_func(func_data->ins, user_data...); }; - std::apply(apply_f, std::any_cast>(func_data->func)); + std::apply(apply_f, std::any_cast>(func_data->func)); }; return static_cast(g_signal_connect(G_OBJECT(t_widget->GetWidget()), Converter::Convert::GetInstance().GetGtkCode(event), G_CALLBACK((void(*)(GtkWidget*, GdkEvent*, PassingDataByFunc*))callback), pass)); @@ -478,16 +479,16 @@ namespace CGui { std::make_tuple(func, &args...), t_widget }; - Storage::GetInstance().Add("Instance", pass, "deleteonquit"); + DeleteOnQuit::GetInstance().Add(pass); - auto callback = [](GtkWidget * widget, GdkEvent * user_event, PassingDataByFunc * func_data) + auto callback = [](GtkWidget* widget, GdkEvent* user_event, PassingDataByFunc* func_data) { - auto apply_f = [](void(*user_func)(Args * ...), Args * ... user_data) + auto apply_f = [](void(*user_func)(Args* ...), Args* ... user_data) { user_func(user_data...); }; - std::apply(apply_f, std::any_cast>(func_data->func)); + std::apply(apply_f, std::any_cast>(func_data->func)); }; return static_cast(g_signal_connect(G_OBJECT(t_widget->GetWidget()), Converter::Convert::GetInstance().GetGtkCode(event), G_CALLBACK((void(*)(GtkWidget*, GdkEvent*, PassingDataByFunc*))callback), pass));