[Rpm-maint] rpmdbNextIterator: memory leakage?

Pazzo Da Legare pazzodalegare at gmail.com
Wed Dec 19 14:34:08 UTC 2007


Dear All again,

In my previous message I asked about querying and freeing
resource....while I'm waiting for your help I'm still doing some test.
Surely I'm wrong but it seems to me there is a memory leakage in the
rpmdbNextIterator()....in my configuration it leaks 990 bytes each
time I call the function dumpPackages...or probably I'm wrong...

I hope you could help me,

pazzodalegare



I'm using the following code:

--------------------------BEGIN EXAMPLE----------------------------
//in rpmquery.c
char **dumpPackages(int *numPkg, char **perrMsg, int onlypkgName){
	Header h;
	rpmts ts;
	int rc, MaxPkg = 1000, l, MaxPkgIncr=500;
	char *name, *ver, *rel, **pkgs=NULL, **tmpPkgs;

	*numPkg = 0; *perrMsg = NULL;

	//necessario per impostare il dbpath
	rpmReadConfigFiles( NULL, NULL );
	ts = rpmtsCreate();
	rc = rpmtsOpenDB(ts, O_RDONLY);

	if( rc != 0 ) {
		fprintf( stderr, "cannot open RPM database.\n" );
		return ( pkgs );
	}

	rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMTAG_NAME, NULL, 0);
	tmpPkgs = (char **)calloc(MaxPkg, sizeof(char *));

	while( (h=rpmdbNextIterator(mi) )!=NULL ) {
		if(h){
			name=NULL; ver=NULL; rel=NULL;
			headerNVR(h,(const char **)&name, (const char **)&ver, (const char
**) &rel );
			//printf("Name %s; Version %s; Release %s", name, ver ,rel);
			l=(int)(strlen(name)+strlen(ver)+strlen(rel)+2+1);
			tmpPkgs[(*numPkg)] = malloc( l*sizeof(char) );

			if(onlypkgName) snprintf( tmpPkgs[(*numPkg)],l,"%s", name);
			else snprintf( tmpPkgs[(*numPkg)],l,"%s-%s-%s",name,ver,rel);
			(*numPkg)++;
			if(*numPkg >= MaxPkg){
				MaxPkg += MaxPkgIncr;
				tmpPkgs = realloc(tmpPkgs, sizeof(char *)*MaxPkg);
			}
		}
	}
	pkgs = realloc(tmpPkgs, (*numPkg)*sizeof(char *) );
	rpmdbFreeIterator(mi);
	rpmtsCloseDB(ts);
	ts = rpmtsFree(ts);
	return pkgs;
}

// in main.c


void freevec(void **v, int l){
	int i;
	if(v==NULL || l<=0)
		return;
	for(i=0; i<l; i++)
		free(v[i]);
	free(v);
}

int main(int argc, char *argv[]){
	int n, errcode,i;
	char *errmsg, **files;
	printf("\n");
	for(i=0; i<1; i++){
		files = dumpPackages(&n, &errmsg, 0);
		freevec((void *)files, n);
	}
        return 0;
}
---------------------------END EXAMPLE

I was looking for memory leakage with valgrind and I got:

 valgrind --leak-check=yes   Debug/test_spxpmsutil  > /tmp/noheaderFree.log
==14158== Memcheck, a memory error detector.
==14158== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al.
==14158== Using LibVEX rev 1658, a library for dynamic binary translation.
==14158== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP.
==14158== Using valgrind-3.2.1, a dynamic binary instrumentation framework.
==14158== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al.
==14158== For more details, rerun with: -v
==14158==
==14158==
==14158== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 31 from 1)
==14158== malloc/free: in use at exit: 68,531 bytes in 2,984 blocks.
==14158== malloc/free: 35,750 allocs, 32,766 frees, 40,975,637 bytes allocated.
==14158== For counts of detected errors, rerun with: -v
==14158== searching for pointers to 2,984 not-freed blocks.
==14158== checked 732,568 bytes.
==14158==
==14158== 990 bytes in 20 blocks are definitely lost in loss record 18 of 32
==14158==    at 0x4005400: malloc (vg_replace_malloc.c:149)
==14158==    by 0xBCD340: pgpPrtUserID (in /usr/lib/librpmio-4.4.so)
==14158==    by 0xBCF487: pgpPrtPkt (in /usr/lib/librpmio-4.4.so)
==14158==    by 0xBCF5CC: pgpPrtPkts (in /usr/lib/librpmio-4.4.so)
==14158==    by 0xD1711A: rpmtsFindPubkey (in /usr/lib/librpm-4.4.so)
==14158==    by 0xD188F3: (within /usr/lib/librpm-4.4.so)
==14158==    by 0xD1A07B: rpmVerifySignature (in /usr/lib/librpm-4.4.so)
==14158==    by 0xCF1BE1: headerCheck (in /usr/lib/librpm-4.4.so)
==14158==    by 0x4042374: rpmdbNextIterator (in /usr/lib/librpmdb-4.4.so)
==14158==    by 0x804DF8A: dumpPackages (rpmqueryop.c:290)
==14158==    by 0x804E027: main (main.c:17)
==14158==
==14158== LEAK SUMMARY:
==14158==    definitely lost: 990 bytes in 20 blocks.
==14158==      possibly lost: 0 bytes in 0 blocks.
==14158==    still reachable: 67,541 bytes in 2,964 blocks.
==14158==         suppressed: 0 bytes in 0 blocks.
==14158== Reachable blocks (those to which a pointer was found) are not shown.
==14158== To see them, rerun with: --show-reachable=yes



More information about the Rpm-maint mailing list