[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: Am I avoiding memory leaks?
- From: Jeff Johnson <jbj JBJ ORG>
- To: rpm-list redhat com
- Subject: Re: Am I avoiding memory leaks?
- Date: Thu, 5 Jul 2001 12:40:54 -0400
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
--
Jeff Johnson ARS N3NPQ
jbj@jbj.org (jbj@redhat.com)
Chapel Hill, NC
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
[]