Hi,
I am using libvirt api to create an app, I am facing a problem.
I can connect to connection object 20 number of times in programm after that it gives connection object null.
Here is the sample code.
Normally code should run 1000 times but it only runs 20 times
Filename: list.cpp
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include <iostream>
#include<string.h>
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
static int count;
static void customErrorFunc(void *userdata, virErrorPtr err)
{
}
int main()
{
virConnectPtr conn;
for ( int i=0; i< 1000;i++)
{
conn = virConnectOpen("qemu:///system");
if (conn == NULL) {
fprintf(stderr, "Failed to open connection to qemu:///system\n");
printf("Failed to open connection to qemu:///system\n");
virConnectClose(conn);
return NULL;
}
printf("Hello %d\n",i);
virError err;
virSetErrorFunc(NULL, customErrorFunc);
int j;
int numDomains1,numDomains2,numDomains;
int *activeDomains;
numDomains1 = virConnectNumOfDomains(conn);
activeDomains = (int*)malloc(sizeof(int) * numDomains1);
numDomains1 = virConnectListDomains(conn, activeDomains, numDomains1);
for (int j = 0 ; j < numDomains1 ; j++)
{
virDomainPtr dom;
dom = virDomainLookupByID(conn,activeDomains[j]);
free(dom);
}
free(activeDomains);
count--;
virConnectClose(conn);
}
}
Output:
Hello 0
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Hello 6
Hello 7
Hello 8
Hello 9
Hello 10
Hello 11
Hello 12
Hello 13
Hello 14
Hello 15
Hello 16
Hello 17
Hello 18
Hello 19
Failed to open connection to qemu:///system
Failed to open connection to qemu:///system
Can any one tell me where I am going wrong.
Regards,
Pankaj Rawat
NECHCL ST