|
Boost Users : |
Subject: Re: [Boost-users] Question about boost::locale translations
From: Dominique Devienne (ddevienne_at_[hidden])
Date: 2012-11-06 04:28:29
On Mon, Nov 5, 2012 at 7:19 PM, Bastien Montagne <montagne29_at_[hidden]>wrote:
> const char* boost_locale_gettext(const char *msgid)
> {
> return boost::locale::gettext(msgid).**c_str();
> }
>
The line above decomposes so:
1) std::string hidden_tmp1 = boost::locale::gettext(msgid)
2) const char* hidden_tmp2 = hidden_tmp1.**c_str() // get pointer to
internal buffer
3) ; // delete hidden_tmp1, which frees buffer hidden_tmp2 points to.
Temporaries live until the semi-colon.
4) return hidden_tmp2 // pointer to freed (stale) memory. Not good.
> I'm quite new to all this C++2C stuff (and to boost too), so please
> forgive me if this is a noob question...
>
You need to strdup() the c_str(), return a pointer to that, *and* document
that callers are responsible for freeing the memory returned to them. Or
your wrapper keeps track of this memory itself somehow, and releases down
the line somehow again, so the callers do not need to explicitly free those
strings. --DD
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net