[rkward] rkward/misc: Fix another memory leak by adding a proper destructor.

Thomas Friedrichsmeier null at kde.org
Fri Oct 12 20:06:20 BST 2018


Git commit 7101f4ad5f5b0017b3ee84f772dc2866a9fbd905 by Thomas Friedrichsmeier.
Committed on 12/10/2018 at 19:03.
Pushed by tfry into branch 'master'.

Fix another memory leak by adding a proper destructor.

This one (failure to delete message catalog handles) was pretty benign, as catalogs
do not be re-allocated at runtime, but it still cleans up the valgrind logs.

M  +20   -8    rkward/misc/rkmessagecatalog.cpp
M  +11   -5    rkward/misc/rkmessagecatalog.h

https://commits.kde.org/rkward/7101f4ad5f5b0017b3ee84f772dc2866a9fbd905

diff --git a/rkward/misc/rkmessagecatalog.cpp b/rkward/misc/rkmessagecatalog.cpp
index db737d3d..56a89a0e 100644
--- a/rkward/misc/rkmessagecatalog.cpp
+++ b/rkward/misc/rkmessagecatalog.cpp
@@ -2,7 +2,7 @@
                           rkmessagecatalog  -  description
                              -------------------
     begin                : Mon Jun 24 2013
-    copyright            : (C) 2013, 2014 by Thomas Friedrichsmeier
+    copyright            : (C) 2013-2018 by Thomas Friedrichsmeier
     email                : thomas.friedrichsmeier at kdemail.net
  ***************************************************************************/
 
@@ -25,9 +25,7 @@
 #include "../debug.h"
 
 // statics
-QHash<QString, RKMessageCatalog*> RKMessageCatalog::catalogs;
-QMutex RKMessageCatalog::setup_mutex;
-RKMessageCatalog* RKMessageCatalog::null_catalog = 0;
+RKMessageCatalog::CatalogHash RKMessageCatalog::catalogs;
 
 RKMessageCatalog::RKMessageCatalog (const QString &name, const QString& path) {
 	RK_TRACE (MISC);
@@ -84,8 +82,7 @@ QString RKMessageCatalog::translate (const QString &msgid_singular, const QStrin
 	return QString::fromUtf8 (dngettext (catalog_name, msgid_singular.toUtf8 (), msgid_plural.toUtf8 (), count)).replace (QLatin1String ("%1"), QString::number (count));
 }
 
-// static
-RKMessageCatalog* RKMessageCatalog::getCatalog (const QString& name, const QString& pathhint) {
+RKMessageCatalog* RKMessageCatalog::CatalogHash::getCatalog (const QString& name, const QString& pathhint) {
 	RK_TRACE (MISC);
 
 	RKMessageCatalog *ret = catalogs.value (name, 0);
@@ -101,10 +98,25 @@ RKMessageCatalog* RKMessageCatalog::getCatalog (const QString& name, const QStri
 	return ret;
 }
 
+RKMessageCatalog::CatalogHash::~CatalogHash() {
+	RK_TRACE (MISC);
+
+	QHash<QString, RKMessageCatalog*>::const_iterator it;
+	for (it = catalogs.constBegin (); it != catalogs.constEnd (); ++it) {
+		delete (it.value ());
+	}
+}
+
+// static
+RKMessageCatalog* RKMessageCatalog::getCatalog (const QString& name, const QString& pathhint) {
+	RK_TRACE (MISC);
+
+	return catalogs.getCatalog (name, pathhint);
+}
+
 RKMessageCatalog* RKMessageCatalog::nullCatalog () {
 	// ok, not thread-safe, here, but the worst that can happen is creating more than one dummy catalog.
-	if (!null_catalog) null_catalog = getCatalog  ("rkward_dummy", QString ());
-	return null_catalog;
+	return (getCatalog  ("rkward_dummy", QString ()));
 }
 
 #ifdef Q_OS_WIN
diff --git a/rkward/misc/rkmessagecatalog.h b/rkward/misc/rkmessagecatalog.h
index d62ee862..4330791d 100644
--- a/rkward/misc/rkmessagecatalog.h
+++ b/rkward/misc/rkmessagecatalog.h
@@ -2,7 +2,7 @@
                           rkmessagecatalog  -  description
                              -------------------
     begin                : Mon Jun 24 2013
-    copyright            : (C) 2013, 2014 by Thomas Friedrichsmeier
+    copyright            : (C) 2013-2018 by Thomas Friedrichsmeier
     email                : thomas.friedrichsmeier at kdemail.net
  ***************************************************************************/
 
@@ -38,7 +38,7 @@ public:
 /** Get the catalog identified by name. This could be an already open catalog, or a new one. In the latter case, the catalog is expected at pathhint. In the former case, pathhint is ignored. This function is guaranteed to return a non-null RKMessageCatalog, although that does not imply the catalog could actually be loaded. */
 	static RKMessageCatalog *getCatalog (const QString &name, const QString &pathhint);
 /** Returns a dummy null-catalog */
-	static RKMessageCatalog *nullCatalog ();
+       static RKMessageCatalog *nullCatalog ();
 /** Switch language to use for any coming translations */
 	static void switchLanguage (const QString &new_language_code);
 private:
@@ -47,9 +47,15 @@ private:
 
 	QByteArray catalog_name;
 
-	static QHash<QString, RKMessageCatalog*> catalogs;
-	static QMutex setup_mutex;
-	static RKMessageCatalog *null_catalog;
+	class CatalogHash {
+		QHash<QString, RKMessageCatalog*> catalogs;
+		QMutex setup_mutex;
+	public:
+		CatalogHash () {};
+		~CatalogHash ();
+		RKMessageCatalog* getCatalog (const QString& name, const QString& pathhint);
+	};
+	static CatalogHash catalogs;
 };
 
 #endif



More information about the rkward-tracker mailing list