Product SiteDocumentation Site

7.6. Interface object memory management

Any time an application calls a function that returns a virInterfacePtr, it is implied that a reference counter has been incremented for that particular interface object. To decrement the reference counter (eventually resulting in the interface object's resources being freed), call virInterfaceFree. This reference counting assures that the interface object will not be freed while an application is still using it.
For cases where an application makes a copy of a virinterfacePtr and stores it away somewhere which may require a lifetime longer than that of the original virinterfacePtr, virinterfaceRef should be called to manually increment the reference count (and virinterfaceFree should be called an extra time, when that copy of the virInterfacePtr is no longer being used).
virInterfacePtr iface;

iface = virInterfaceLookupByName("eth0");

mydata.iface = iface;
virInterfaceRef(mydata.iface);
/* now we're done with iface */
virInterfaceFree(iface);

...

/* now we're done with mydata.iface */
virInterfaceFree(mydata.iface);
Example 7.14. Reference counting an interface object