[libvirt] [libvirt-glib 1/6] Getters for GVirConfigDomainInterface attributes

Christophe Fergeau cfergeau at redhat.com
Wed Feb 29 14:22:23 UTC 2012

On Wed, Feb 29, 2012 at 04:10:29PM +0200, Zeeshan Ali (Khattak) wrote:
> Ah, I just realized why that is so:
> static char *libxml_str_to_glib(xmlChar *str)
> {
>     char *g_str;
>     if (str == NULL)
>         return NULL;
>     g_str = g_strdup((char *)str);
>     xmlFree(str);
>     return g_str;
> }
> This function is not needed as all you needed was to cast the 'xmlChar
> *' to 'const gchar *' and return const from all users of this
> function. Since we still are not API/ABI stable, I propose we change
> this all over as there is no need to force apps to free strings all
> the time and waste processor/memory on all these string
> allocation/de-allocation.

You'll still need to free the input "str", and you have no guarantee that
xmlFree and g_free will call the same function to free memory in the end,
especially in a library. This function is sucky, but there is no clean way
around it as far as I know.
If you really insist on returning const from your getters, you'll need to
cache their value in GVirDomainDevicePrivate

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120229/d361916c/attachment-0001.sig>

More information about the libvir-list mailing list