memory leak - Mailing list pgsql-general

From Michael P. Soulier
Subject memory leak
Date
Msg-id 20120110184858.GC8167@mitel.com
Whole thread Raw
Responses Re: memory leak  (Simon Riggs <simon@2ndQuadrant.com>)
List pgsql-general
Hi,

I've written a small multi-threaded C program using libpq, and valgrind is
reporting a memory leak.

2012-01-10 13:45:07.263078500 ==12695== 608 bytes in 4 blocks are definitely lost in loss record 19 of 22
2012-01-10 13:45:07.263097500 ==12695==    at 0x4005B83: malloc (vg_replace_malloc.c:195)
2012-01-10 13:45:07.263116500 ==12695==    by 0x34AD94: PQmakeEmptyPGresult (in /usr/lib/libpq.so.5.2)
2012-01-10 13:45:07.263133500 ==12695==    by 0x35466F: ??? (in /usr/lib/libpq.so.5.2)
2012-01-10 13:45:07.263149500 ==12695==    by 0x349E83: ??? (in /usr/lib/libpq.so.5.2)
2012-01-10 13:45:07.263167500 ==12695==    by 0x34B008: PQgetResult (in /usr/lib/libpq.so.5.2)
2012-01-10 13:45:07.263184500 ==12695==    by 0x34B26B: ??? (in /usr/lib/libpq.so.5.2)
2012-01-10 13:45:07.263201500 ==12695==    by 0x8048E1C: ??? (in /usr/sbin/tug-queue-counter)
2012-01-10 13:45:07.263218500 ==12695==    by 0x8049029: ??? (in /usr/sbin/tug-queue-counter)
2012-01-10 13:45:07.263236500 ==12695==    by 0xC05831: start_thread (in /lib/libpthread-2.5.so)
2012-01-10 13:45:07.263254500 ==12695==    by 0xB3046D: clone (in /lib/libc-2.5.so)

Now, I'm trying to figure out what I'm doing wrong. I'm working from example
code found on the postgres site...

This thread wakes up periodically to update a db value like so...

void
update_queue_size(int size)
{
    // Critical section around event_count
    pthread_mutex_lock(&counter_mutex);
    printf("DB: Setting event_queue_size to %ld\n", event_count);
    sprintf(commandbuf, command_format, event_count);
    printf("DB: Command is: %s\n", commandbuf);

    res = PQexec(conn, "BEGIN");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "DB: BEGIN command failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }

    res = PQexec(conn, commandbuf);
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "DB: UPDATE command failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }

    /* end the transaction */
    res = PQexec(conn, "END");
    PQclear(res);
    // end critical section
    pthread_mutex_unlock(&counter_mutex);
}

where exit_nicely() is

void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}

Can anyone point out improper use of libpq here?

Thanks,
mps

pgsql-general by date:

Previous
From: Scott Marlowe
Date:
Subject: Re: How do you change the size of the WAL files?
Next
From: Simon Riggs
Date:
Subject: Re: memory leak