memory leak in threaded getpwuid_r() usage

Discussion in 'C Programming' started by knt, Jan 15, 2006.

  1. knt

    knt Guest

    Hi,

    Trying to use getpwuid_r() in a threaded application. (Code is attached
    at the end of the message.) But in the "ps -p <PID> -o rss,size" output
    of the program, RSS increases by time while SIZE remains stable.

    Here's the (ps -p <PID> -o rss,size) output of the program:

    [BEGIN:eek:utput]
    $ ./a.out
    user_id = 1000, proc_id = 28583
    1 700 46336
    2 704 46336
    ....
    47 716 46332
    48 716 46332
    49 716 46336
    50 716 46336
    [END:eek:utput]

    And valgrind --leak-check=full output:

    [BEGIN:valgrind]
    156 (36 direct, 120 indirect) bytes in 1 blocks are definitely lost in
    loss record 4 of 9
    at 0x1B90459D: malloc (vg_replace_malloc.c:130)
    by 0x1BA0BEE6: (within /lib/tls/libc-2.3.2.so)
    by 0x1BA0B788: __nss_database_lookup (in /lib/tls/libc-2.3.2.so)
    by 0x1DB70AFB: ???
    by 0x1B9CCD4B: getpwuid_r (in /lib/tls/libc-2.3.2.so)
    by 0x804879A: sub_func (getpwuid.c:43)
    by 0x1B918B62: start_thread (in /lib/tls/libpthread-0.60.so)
    by 0x1B9FC189: clone (in /lib/tls/libc-2.3.2.so)

    340 bytes in 5 blocks are possibly lost in loss record 8 of 9
    at 0x1B904F75: calloc (vg_replace_malloc.c:175)
    by 0x1B8F2678: (within /lib/ld-2.3.2.so)
    by 0x1B8F294B: _dl_allocate_tls (in /lib/ld-2.3.2.so)
    by 0x1B91924A: allocate_stack (in /lib/tls/libpthread-0.60.so)
    by 0x1B918C54: pthread_create@@GLIBC_2.1 (in
    /lib/tls/libpthread-0.60.so)
    by 0x80488AB: main (getpwuid.c:68)
    [END:valgrind]

    I'd be so appreciated if somebody can help me to figure out the problem
    in the code. (Suggesting mailing lists that I can ask this problem is
    welcome too.)


    Regards.

    [BEGIN:getpwuid.c]
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <pwd.h>

    #include "pthread.h"

    uid_t user_id;
    pid_t proc_id;
    char cmd[128];
    int iter;

    void
    print_mem_usage(void)
    {
    FILE *fp = popen(cmd, "r");
    static char buf[64];

    if (!fp)
    {
    fprintf(stderr, "popen() failed!\n");
    return;
    }

    (void) fgets(buf, sizeof(buf), fp);
    fgets(buf, sizeof(buf), fp);
    printf("%3d %s", iter++, buf);

    fclose(fp);
    }

    void *
    sub_func(void *tmp)
    {
    int ret;
    size_t sz = 128;
    char buf[sz];

    struct passwd old_pw;
    struct passwd *new_pw;

    ret = getpwuid_r(user_id, &old_pw, buf, sz, &new_pw);
    if (ret)
    fprintf(stderr, "getpwuid_r() failed! ret = %d\n", ret);

    pthread_exit(NULL);
    }

    int
    main(void)
    {
    int loop_count = 50;
    int thread_count = 100;
    pthread_t threads[thread_count];
    int i, ret;

    user_id = getuid();
    proc_id = getpid();
    sprintf(cmd, "ps -p %d -o rss,size", proc_id);
    iter = 1;
    printf("user_id = %d, proc_id = %d\n", user_id, proc_id);

    while (loop_count--)
    {
    for (i = 0; i < thread_count; i++)
    {
    ret = pthread_create(&threads, NULL, sub_func, NULL);
    if (ret)
    fprintf(stderr, "pthread_create() failed! i = %d, ret =
    %d\n", i, ret);
    }

    for (i = 0; i < thread_count; i++)
    {
    ret = pthread_join(threads, NULL);
    if (ret)
    fprintf(stderr, "pthread_join() failed! i = %d, ret =
    %d\n", i, ret);
    }

    print_mem_usage();
    }

    return 0;
    }
    [END:getpwuid.c]
     
    knt, Jan 15, 2006
    #1
    1. Advertising

  2. knt

    Artie Gold Guest

    knt wrote:
    > Hi,
    >
    > Trying to use getpwuid_r() in a threaded application. (Code is attached
    > at the end of the message.) But in the "ps -p <PID> -o rss,size" output
    > of the program, RSS increases by time while SIZE remains stable.
    >
    > Here's the (ps -p <PID> -o rss,size) output of the program:
    >


    [snip]

    Whoa. You're well outside the bounds of news:comp.lang.c (which covers
    the ISO standard C language). May I suggest news:comp.unix.prorammer or
    news:comp.os.linux.development.apps?

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Jan 15, 2006
    #2
    1. Advertising

  3. knt

    knt Guest

    Thanks so much for your warning. You're quite right. I've moved
    question to comp.unix.prorammer.


    Regards.
     
    knt, Jan 15, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richie

    ASPNET_WP memory usage...Leak ???

    Richie, Nov 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    722
    Kevin Spencer
    Nov 4, 2004
  2. metfan
    Replies:
    2
    Views:
    4,869
    Robert Olofsson
    Oct 21, 2003
  3. cakmak
    Replies:
    1
    Views:
    557
    Andrew Thompson
    Jan 10, 2007
  4. hvt
    Replies:
    0
    Views:
    1,224
  5. Mark Probert
    Replies:
    4
    Views:
    341
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page