[Rpm-maint] how to free memory after querying installed pkgs....

Pazzo Da Legare pazzodalegare at gmail.com
Wed Dec 19 11:58:10 UTC 2007

Dear all,

I want to make simple query on pkgs in rpmdb. I wrote this simple
function...but I don't understand how to free resources. I tried this
function on every package querying for pkg description and pkg
summary...but I have a memory leakage. Could you help me? If I use the
headerFree() function after rpmdbFreeIterator() I got a segmentation



char *queryPackage(char *pkgName, const char *query,  char **perrMsg){
	Header h;
	rpmts ts;
	int rc;
	char *sResult=NULL, *errMsg=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" );
		*perrMsg = (char *)rpmErrorString();
		return ( NULL );

	rpmdbMatchIterator mi = rpmtsInitIterator(ts, RPMTAG_NAME, pkgName, 0);

	while( (h=rpmdbNextIterator(mi) )!=NULL ) {
		sResult = headerSprintf(h, query, rpmTagTable, rpmHeaderFormats,
(errmsg_t *)&errMsg);
				break; //only one pkg with pkgName		
	//headerFree(h)       //<- this give me a segmentation fault
	ts = rpmtsFree(ts);
	*perrMsg = errMsg;
	return sResult;

More information about the Rpm-maint mailing list