[libvirt-users] undefining and redefining a Domain

<resending as I don't think I was in the mailing list yet when I sent first time... apologies if you receive this twice> 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

2010/10/1 Roberto Attias (rattias) <rattias@cisco.com>:
<resending as I don’t think I was in the mailing list yet when I sent first time… apologies if you receive this twice>
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);
}
}
You're leaking a virDomainPtr here. Call virDomainFree(dom) before overwriting the pointer with the result from virDomainDefineXML. I think the leaked virDomainPtr causes the trouble you describe. So fixing the leak will probably also fix the problem you see in the second run.
dom = virDomainDefineXML(virt, SOME_XML);
Matthias

Yes, I was calling free() instead of virDomainFree. Thanks! -----Original Message----- From: Matthias Bolte [mailto:matthias.bolte@googlemail.com] Sent: Friday, October 01, 2010 9:11 AM To: Roberto Attias (rattias) Cc: libvirt-users@redhat.com Subject: Re: [libvirt-users] undefining and redefining a Domain 2010/10/1 Roberto Attias (rattias) <rattias@cisco.com>:
<resending as I don’t think I was in the mailing list yet when I sent first time… apologies if you receive this twice>
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);
}
}
You're leaking a virDomainPtr here. Call virDomainFree(dom) before overwriting the pointer with the result from virDomainDefineXML. I think the leaked virDomainPtr causes the trouble you describe. So fixing the leak will probably also fix the problem you see in the second run.
dom = virDomainDefineXML(virt, SOME_XML);
Matthias
participants (2)
-
Matthias Bolte
-
Roberto Attias (rattias)