Re: g_utf16_to_utf8



Hello all,

> I thought (past of think?) that freeing the pointer, the ustring becomes to
> point to garbage.
> The code below is right?
> 
> Glib::ustring win_NetworkInterface::get_name()
> {
>     gchar* pt_name = g_utf16_to_utf8(
>             (gunichar2*) this->ifinfo.FriendlyName, -1, NULL, NULL, NULL);
>     Glib::ustring name(pt_name);
>     g_free(pt_name);
>     return name;
> }

The code is correct as long as no exception occurs within 
ustring::ustring(), which can happen for instance in low memory 
conditions.

Your idea to use a smart pointer (RefPtr<> in your example) was a smart
idea for catching exceptional situations. But you need to use a smart
pointer that will call g_free() instead of, for instance, calling delete.

Either write one for this particular case (it's only a few lines), or find
one that uses g_free(). Or use a smart pointer that can be _configured_ to
call the proper function. The Loki library contains a template class
SmartPtr<> that can be configured with template arguments (classes) to
call the free-Funktion of your choise (free(), g_free(), delete, delete[],
or even do nothing), how to react to NULL-pointers (allow, throw
exception, assert, use default value instead) and a few more things.

Hope this helps somebody. Have a good week!
Mark Roberts


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]