[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Am I avoiding memory leaks?



This was very helpful, just one question for my clarification about rpmReadConfigFiles. Does this function only need to be called once for the duration of my program? I guess I'd better look at the source to see what it does. I'm assuming it reads the rc files and sets up an evnvironment?

Perhaps for the clueless like me, a little more in the documentation (doxygen) than "Read macro configuration file(s) for a target." as the description for this function would be helpful to become more clueful. Just a thought.

Thanks again for your help,

-- Dan

On Thu, 5 Jul 2001, Jeff Johnson wrote:

> On Thu, Jul 05, 2001 at 10:20:22AM +0000, hanksdc@about-inc.com wrote:
> > Just wanted to pose some of my code to this group. I'm writing a program that's using rpmlib, and I'm pretty familliar with C, but not all that experienced. I just wanted to pose one of my functions to the group to see if you can see any memory leaks that I am missing. It seems when I run the program, anytime I go over this loop, the memory size of the program increases, but it seems to me I'm freeing eveything I allocate. Am I missing something?
> >
> > Here's my code: It takes a socket as an argument, to which I spew the results of my database query.
> >
> > int QA_command( int socket ) {
> >   Header h;
> >   rpmdb db;
> >   rpmdbMatchIterator mi = NULL;
> >   char *name, *version, *release;
> >   char send_line[ SEND_LINE_SIZE ];
> >
> >   rpmReadConfigFiles( NULL, NULL );
> ----^^^^^^^^^^^^^^^^^^
>
> This routine is beacoup stateful, lots of mallocs here.
>
> You can try trashing and burning the rpm configuration environment if
> you want, see the end of main() in rpmqv.c for most of the details.
> However, there are still a couple memory leaks (2-3 IIRC) that are
> very hard to plug in rpmReadConfigFiles(). Those leaks will get you eventually,
> if not sooner.
>
> Probably the simplest fix is something like
>
>     static int _initialised = 0;
>
>     if (!initialized) {
> 	rpmReadConfigFiles( NULL, NULL );
> 	initialized = 1;
>     }
>
> Another simple fix is to figger what macros you actually need from
> the configuration, and just add them without reading all the
> rest of the glop. I don't believe you need much more than
>
>     if (!initialized) {
> 	rpmDefineMacro(NULL, "%_dbpath /var/lib/rpm", -1);
> 	initialized = 1;
>     }
> (untested, you know what I mean :-) but the way to find out is ...
>
> >   if( rpmdbOpen( "", &db, O_RDONLY, 0644 ) != 0 ) {
> >     perror( "rpmdbOpen failed" );
> >     send( socket, DB_OPEN_ERR, DB_OPEN_ERR_LEN, 0 );
> >     return ( COMMAND_FAIL );
> >   }
> >
> >   mi = rpmdbInitIterator( db, RPMDBI_PACKAGES, NULL, 0 );
> >
> >   while( h = rpmdbNextIterator( mi ) ) {
> >     if( h != NULL ) {
> >       headerGetEntry( h, RPMTAG_NAME, NULL, (void **) &name, NULL );
> >       headerGetEntry( h, RPMTAG_VERSION, NULL, (void **) &version, NULL );
> >       headerGetEntry( h, RPMTAG_RELEASE, NULL, (void **) &release, NULL );
> >
> >       snprintf(send_line, SEND_LINE_SIZE, "%s-%s-%s\n", name, version, release);
> >       send( socket, send_line, strnlen( send_line, SEND_LINE_SIZE ), 0 );
> >     }
> >   }
> >   headerFree( h );
> >   rpmdbFreeIterator( mi );
> >   rpmdbClose( db );
>
> ... to call
> 	rpmDumpMacroTable(NULL, NULL);
> here and examine stderr for macros that were actually used during your
> subroutine traversal. Look for a leading '=' rather than a ':' in the output
> for macros that were used.
>
>
> >   return ( COMMAND_SUCCESS );
> > }
> >
>
> HTH
>
> 73 de Jeff
>
>

-- 
========================================================================
   Daniel Hanks - Systems/Database Administrator
   About Inc., Web Services Division
   1253 N. Research Way, Suite Q-2500.  Orem, UT 84097
   ph: 801-437-6023  fax: 801-437-6020  email: hanksdc@about-inc.com
========================================================================






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index] []