diff options
author | Andreas Nordal <andreas_nordal_4@hotmail.com> | 2016-03-18 23:14:16 +0100 |
---|---|---|
committer | ridiculousfish <corydoras@ridiculousfish.com> | 2016-03-27 17:40:48 -0700 |
commit | 6495bd470d40b2b489e6fe350e2da08be64d4bec (patch) | |
tree | 79d8821e2016cdf863ffa247ee0a3a24886add1a /src/wutil.cpp | |
parent | 7accadc33f817a3e17c14c989a3b83f0d6737665 (diff) |
Fix memory leaks at exit found in tests
This fixes all memory leaks found by compiling with
clang++ -g -fsanitize=address and running the tests.
Method:
Ensure that memory is freed by the destructor of its respective container,
either by storing objects directly instead of by pointer, or implementing
the required destructor.
Diffstat (limited to 'src/wutil.cpp')
-rw-r--r-- | src/wutil.cpp | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/src/wutil.cpp b/src/wutil.cpp index 2ff358bc..62602c2c 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -44,8 +44,8 @@ const file_id_t kInvalidFileID = {(dev_t)-1LL, (ino_t)-1LL, (uint64_t)-1LL, -1, /* Lock to protect wgettext */ static pthread_mutex_t wgettext_lock; -/* Maps string keys to (immortal) pointers to string values. */ -typedef std::map<wcstring, const wchar_t *> wgettext_map_t; +/* Map used as cache by wgettext. */ +typedef std::map<wcstring, wcstring> wgettext_map_t; static wgettext_map_t wgettext_map; bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &out_name, bool *out_is_dir) @@ -488,16 +488,18 @@ const wchar_t *wgettext(const wchar_t *in) wcstring key = in; scoped_lock lock(wgettext_lock); - // Reference to pointer to string - const wchar_t *& val = wgettext_map[key]; - if (val == NULL) + wcstring &val = wgettext_map[key]; + if (val.empty()) { cstring mbs_in = wcs2string(key); char *out = fish_gettext(mbs_in.c_str()); - val = wcsdup(format_string(L"%s", out).c_str()); //note that this writes into the map! + val = format_string(L"%s", out).c_str(); } errno = err; - return val; //looks dangerous but is safe, since the string is stored in the map + + // The returned string is stored in the map + // TODO: If we want to shrink the map, this would be a problem + return val.c_str(); } int wmkdir(const wcstring &name, int mode) |