I suspected something like that (and indeed it worked with a strdup). Unfortunately, I’m not sure there will be a nice and suitable way to handle this, will try to find something…

Anyway, thank you very much for the answer! :)

On 06/11/2012 10:28, Dominique Devienne wrote:
On Mon, Nov 5, 2012 at 7:19 PM, Bastien Montagne <montagne29@wanadoo.fr> 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 mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users