Hello,

I’m facing some strange behavior, and I hope you can provide a clarification.

Consider the following code:

 

     virDomainPtr dom = virDomainLookupByName(virt, domain_name);

     if (dom) {

           printf("domain already defined...\n");

           if (virDomainUndefine(dom))

                printf("...unable to undefine!!!\n");

           else {

                printf("...undefined.");

                free(dom);

           }

     }

     dom = virDomainDefineXML(virt, SOME_XML);

     if (dom != NULL) {

           const char *name = virDomainGetName(dom);

           printf("NAME IS NOW: %s\n", name);

     } else

           printf("dom is NULL!\n");

 

When executed the first time, the code correctly defines the domain, and prints:

 

defining domain test2-vm

libvir: QEMU error : Domain not found: no domain with matching name 'test2-vm'

NAME IS NOW: test2-vm

 

If executed a second time with  the same SOME_XML when the domain is already defined, it prints:

 

defining domain test2-vm

domain already defined...

libvir: Domain error : invalid domain pointer in virDomainGetName

undefining......undefined.NAME IS NOW: (null)

 

Not only the name is null, but the domain can’t be started. What is the correct way to undefined programmatically a domain that is already defined? Why is the second virDomainDefineXML() returining a pointer (as opposed to null), but the pointed object seem to be invalid?

 

Thanks,

                 Roberto