mirror of
https://github.com/subsurface/subsurface.git
synced 2025-01-31 20:43:24 +00:00
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 <dirk@hohndel.org>
This commit is contained in:
parent
7813ac86bf
commit
2e43769108
2 changed files with 17 additions and 6 deletions
|
@ -2,9 +2,17 @@
|
|||
#include <QString>
|
||||
#include <gettextfromc.h>
|
||||
|
||||
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()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#ifndef GETTEXT_H
|
||||
#define GETTEXT_H
|
||||
#ifndef GETTEXTFROMC_H
|
||||
#define GETTEXTFROMC_H
|
||||
|
||||
#include <QHash>
|
||||
|
||||
extern "C" const char *gettext(const char *text);
|
||||
|
||||
|
@ -9,7 +10,9 @@ class gettextFromC
|
|||
Q_DECLARE_TR_FUNCTIONS(gettextFromC)
|
||||
public:
|
||||
static gettextFromC *instance();
|
||||
char *gettext(const char *text);
|
||||
const char *gettext(const char *text);
|
||||
void reset(void);
|
||||
QHash <const char *, QByteArray> translationCache;
|
||||
};
|
||||
|
||||
#endif // GETTEXT_H
|
||||
#endif // GETTEXTFROMC_H
|
||||
|
|
Loading…
Add table
Reference in a new issue