From 2e43769108d4023c215451fa8116abdc991e0f19 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 8 Oct 2013 22:44:38 -0700 Subject: Avoid the memory leaks from translations Instead use a hash to cache the translations (and allow for the ability to clear the hash so we can even switch translations at runtime...). Now Qt will keep track of the memory and release it for us when we are done with it. This avoids the memory leak introduced in commit 4ecb35bf5ff2 ("Make a copy of the translated text"). Signed-off-by: Dirk Hohndel --- gettextfromc.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'gettextfromc.cpp') diff --git a/gettextfromc.cpp b/gettextfromc.cpp index fe6263a4b..de278afeb 100644 --- a/gettextfromc.cpp +++ b/gettextfromc.cpp @@ -2,9 +2,17 @@ #include #include -char *gettextFromC::gettext(const char *text) +const char *gettextFromC::gettext(const char *text) { - return strdup(tr(text).toLocal8Bit().data()); + QByteArray &result = translationCache[text]; + if (result.isEmpty()) + result = tr(text).toUtf8(); + return result.constData(); +} + +void gettextFromC::reset(void) +{ + translationCache.clear(); } gettextFromC* gettextFromC::instance() -- cgit v1.2.3-70-g09d2