memory management fixes
authorVolker Birk <vb@pep-project.org>
Sun, 31 May 2015 23:39:18 +0200
changeset 427d1be3be0c07
parent 41 c55eaabd5ea6
child 43 3d403e61e33a
memory management fixes
CpEpEngine.cpp
pEp_utitlity.cpp
utf8_helper.cpp
     1.1 --- a/CpEpEngine.cpp	Sun May 31 22:41:05 2015 +0200
     1.2 +++ b/CpEpEngine.cpp	Sun May 31 23:39:18 2015 +0200
     1.3 @@ -907,15 +907,14 @@
     1.4      ::PEP_color _rating;
     1.5  
     1.6      PEP_STATUS status = ::decrypt_message(get_session(), _src, &msg_dst, &_keylist, &_rating);
     1.7 +
     1.8 +    if (msg_dst)
     1.9 +        text_message_from_C(dst, msg_dst);
    1.10 +    else
    1.11 +        text_message_from_C(dst, _src);
    1.12 +
    1.13      ::free_message(_src);
    1.14 -
    1.15 -    if (msg_dst) {
    1.16 -        text_message_from_C(dst, msg_dst);
    1.17 -        ::free_message(msg_dst);
    1.18 -    }
    1.19 -    else {
    1.20 -        text_message_from_C(dst, _src);
    1.21 -    }
    1.22 +    ::free_message(msg_dst);
    1.23  
    1.24      if (_keylist) {
    1.25          *keylist = string_array(_keylist);
     2.1 --- a/pEp_utitlity.cpp	Sun May 31 22:41:05 2015 +0200
     2.2 +++ b/pEp_utitlity.cpp	Sun May 31 23:39:18 2015 +0200
     2.3 @@ -156,10 +156,10 @@
     2.4  
     2.5          BSTR bstr(char *s)
     2.6          {
     2.7 -            if (s)
     2.8 -                return utf16_bstr(s);
     2.9 -            else
    2.10 +            if (s == NULL)
    2.11                  return _bstr_t(L"").Detach();
    2.12 +
    2.13 +            return utf16_bstr(s);
    2.14          }
    2.15  
    2.16          template<> blob *from_C< blob *, bloblist_t >(bloblist_t *tl)
    2.17 @@ -367,10 +367,10 @@
    2.18                  SafeArrayAccessData(b.value, (void **) &data);
    2.19                  memcpy(buffer, data, size);
    2.20                  SafeArrayUnaccessData(sa);
    2.21 -                free(buffer);
    2.22  
    2.23                  _bl = bloblist_add(_bl, buffer, size, str(b.mime_type), str(b.filename));
    2.24                  if (_bl == NULL) {
    2.25 +                    free(buffer);
    2.26                      clear_blob(b);
    2.27                      free_bloblist(bl);
    2.28                      throw bad_alloc();
     3.1 --- a/utf8_helper.cpp	Sun May 31 22:41:05 2015 +0200
     3.2 +++ b/utf8_helper.cpp	Sun May 31 23:39:18 2015 +0200
     3.3 @@ -49,7 +49,11 @@
     3.4              if (bstr == NULL)
     3.5                  return "";
     3.6  
     3.7 -            return utf8_string((wstring) (wchar_t *) _bstr_t(bstr, true), norm);
     3.8 +            _bstr_t _bstr(bstr);
     3.9 +            wstring wstr = (wchar_t *) _bstr;
    3.10 +            _bstr.Detach();
    3.11 +
    3.12 +            return utf8_string(wstr, norm);
    3.13          }
    3.14  
    3.15          wstring utf16_string(string str)
    3.16 @@ -79,53 +83,53 @@
    3.17              return _bstr_t(wstr.c_str()).Detach();
    3.18          }
    3.19  
    3.20 -        LPSAFEARRAY string_array(const ::stringlist_t *stringlist)
    3.21 -        {
    3.22 -            int len = ::stringlist_length(stringlist);
    3.23 -
    3.24 -            if (len = 0)
    3.25 -                return NULL;
    3.26 -
    3.27 -            CComSafeArray<BSTR> sa_string_list((LONG) len);
    3.28 -            LONG n = 0;
    3.29 -            for (const ::stringlist_t *k = stringlist; k && k->value; k = k->next) {
    3.30 -                if (k->value) {
    3.31 -                    HRESULT _result = sa_string_list.SetAt(n, utf16_bstr(k->value), false);
    3.32 -                    assert(_result == S_OK);
    3.33 -                    if (_result == E_OUTOFMEMORY)
    3.34 -                        throw std::bad_alloc();
    3.35 -                    ++n;
    3.36 -                }
    3.37 -            }
    3.38 -
    3.39 -            return sa_string_list.Detach();
    3.40 -        }
    3.41 -
    3.42 -        ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
    3.43 -        {
    3.44 -            if (safearray == NULL)
    3.45 -                return NULL;
    3.46 -
    3.47 -            CComSafeArray<BSTR> sa(safearray);
    3.48 -            int n_strings = 0;
    3.49 -            ::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
    3.50 -            assert(_stringlist);
    3.51 -            if (_stringlist == NULL)
    3.52 -                throw std::bad_alloc();
    3.53 -
    3.54 -            n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
    3.55 -            ::stringlist_t *k = _stringlist;
    3.56 -            for (int i = 0, j = sa.GetLowerBound(); i < n_strings; ++i, ++j) {
    3.57 -                k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
    3.58 -                assert(k);
    3.59 -                if (k == NULL) {
    3.60 -                    ::free_stringlist(_stringlist);
    3.61 -                    throw std::bad_alloc();
    3.62 -                }
    3.63 -            }
    3.64 -
    3.65 -            return _stringlist;
    3.66 -        }
    3.67 -
    3.68 -    }
    3.69 -}
    3.70 +        LPSAFEARRAY string_array(const ::stringlist_t *stringlist)
    3.71 +        {
    3.72 +            int len = ::stringlist_length(stringlist);
    3.73 +
    3.74 +            if (len == 0)
    3.75 +                return NULL;
    3.76 +
    3.77 +            CComSafeArray<BSTR> sa_string_list((LONG) len);
    3.78 +            LONG n = 0;
    3.79 +            for (const ::stringlist_t *k = stringlist; k && k->value; k = k->next) {
    3.80 +                if (k->value) {
    3.81 +                    HRESULT _result = sa_string_list.SetAt(n, utf16_bstr(k->value), false);
    3.82 +                    assert(_result == S_OK);
    3.83 +                    if (_result == E_OUTOFMEMORY)
    3.84 +                        throw std::bad_alloc();
    3.85 +                    ++n;
    3.86 +                }
    3.87 +            }
    3.88 +
    3.89 +            return sa_string_list.Detach();
    3.90 +        }
    3.91 +
    3.92 +        ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
    3.93 +        {
    3.94 +            if (safearray == NULL)
    3.95 +                return NULL;
    3.96 +
    3.97 +            CComSafeArray<BSTR> sa(safearray);
    3.98 +            int n_strings = 0;
    3.99 +            ::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
   3.100 +            assert(_stringlist);
   3.101 +            if (_stringlist == NULL)
   3.102 +                throw std::bad_alloc();
   3.103 +
   3.104 +            n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
   3.105 +            ::stringlist_t *k = _stringlist;
   3.106 +            for (int i = 0, j = sa.GetLowerBound(); i < n_strings; ++i, ++j) {
   3.107 +                k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
   3.108 +                assert(k);
   3.109 +                if (k == NULL) {
   3.110 +                    ::free_stringlist(_stringlist);
   3.111 +                    throw std::bad_alloc();
   3.112 +                }
   3.113 +            }
   3.114 +
   3.115 +            return _stringlist;
   3.116 +        }
   3.117 +
   3.118 +    }
   3.119 +}