C内存泄漏问题

发布于 2024-11-04 07:07:46 字数 5328 浏览 0 评论 0原文

your program did not free all of the memory it allocated
==17711== Memcheck, a memory error detector
==17711== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==17711== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==17711== Command: ./counter
==17711==
==17711== Conditional jump or move depends on uninitialised value(s)
==17711==    at 0x400B98: main (counter.c:105)
==17711==
==17711== Conditional jump or move depends on uninitialised value(s)
==17711==    at 0x400BDB: main (counter.c:107)
==17711==
==17711== Conditional jump or move depends on uninitialised value(s)
==17711==    at 0x400C1B: main (counter.c:109)
==17711==
error
==17711==
==17711== HEAP SUMMARY:
==17711==     in use at exit: 64 bytes in 4 blocks
==17711==   total heap usage: 4 allocs, 0 frees, 64 bytes allocated
==17711==
==17711== LEAK SUMMARY:
==17711==    definitely lost: 0 bytes in 0 blocks
==17711==    indirectly lost: 0 bytes in 0 blocks
==17711==      possibly lost: 0 bytes in 0 blocks
==17711==    still reachable: 64 bytes in 4 blocks
==17711==         suppressed: 0 bytes in 0 blocks
==17711== Rerun with --leak-check=full to see details of leaked memory
==17711==
==17711== For counts of detected and suppressed errors, rerun with: -v
==17711== Use --track-origins=yes to see where uninitialised values come from
==17711== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 6 from 6)
make: *** [test_counter] Error 1

我在这个程序中存在内存泄漏...我很确定我已经释放了所有动态内存..我不明白泄漏在哪里=\

#include "counter.h"

/* ============================================================================
 * File-global variables
 * ========================================================================== */
static int ncounters = 0;
static struct counter *counters = NULL;

static int nthreads = 0;
static int *ninstructions = NULL;
static struct instruction **instructions = NULL;


/* ============================================================================
 * Operations
 * ========================================================================== */
static void
decrement(long long *n) {
        (*n)--;
}

static void
increment(long long *n) {
    (*n)++;
}

static void
mult2(long long *n) {
    (*n)=(*n)*2;
}


/* ============================================================================
 * Helper functions
 * ========================================================================== */

pthread_mutex_t lock1;

/* ============================================================================
 * Thread function
 * ========================================================================== */
static void *
worker_thread(void *arg) {

        pthread_mutex_lock( &lock1 );

        long long n = (long long)arg;
        for(int i=0; i < ninstructions[n] ;i++){
          for(int j=0; j < instructions[n][i].repetitions ;j++){
            (*instructions[n][i].work_fn)(&((instructions[n][i].counter)->counter));
          }
        }
        pthread_mutex_unlock( &lock1 );
    return NULL;
}


/* ============================================================================
 * Main function
 * ========================================================================== */
int
main(void) {

        scanf(" %d", &ncounters); //Ask for number of counters

        int i;

        if((counters = malloc(sizeof(struct counter)*ncounters))){
          for( i=0; i < ncounters ;i++){
            counters[i].counter = 0;
          }
        }

        scanf(" %d", &nthreads); //Ask for number of threads

        if((ninstructions = (int*) malloc(nthreads*sizeof(int)))){
          for( i=0; i < nthreads ;i++){
            ninstructions[i] = 0;
          }
        }

        instructions = malloc(nthreads*sizeof(struct instruction *));

        for(i=0; i < nthreads ;i++){

          scanf(" %d", &ninstructions[i]);  //Ask for number of instructions within threads[i]

          instructions[i] = malloc(ninstructions[i]*sizeof(struct instruction));
          for(int j=0;j < ninstructions[i] ;j++){
            int Srepetition;
            char Sfunction;
            int Scounter;

            scanf(" %d %c %d", &Scounter, &Sfunction, &Srepetition);

            instructions[i][j].repetitions = Srepetition;

            instructions[i][j].counter = (counters+Scounter);

            if(Sfunction == 'I'){
              instructions[i][j].work_fn = increment;
            }else if(Sfunction == 'D'){
              instructions[i][j].work_fn = decrement;
            }else if(Sfunction == '2'){
              instructions[i][j].work_fn = mult2;
            }else{
              printf("error\n");
            }

          }

        }

        pthread_t thread_id[nthreads];

        for(long long i=0; i < nthreads ;i++){
          pthread_create( &thread_id[i], NULL, worker_thread, (void *)i);
        }

        for(int i=0; i < nthreads ;i++){
          pthread_join( thread_id[i], NULL);
        }

        for(i=0; i < ncounters ;i++){
          printf("%lld\n", counters[i].counter);
        }

        for(int i=0; i< nthreads;i++){
          free(instructions[i]);
        }
        free(instructions);
        free(ninstructions);
        free(counters);
    return 0;
}
your program did not free all of the memory it allocated
==17711== Memcheck, a memory error detector
==17711== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==17711== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==17711== Command: ./counter
==17711==
==17711== Conditional jump or move depends on uninitialised value(s)
==17711==    at 0x400B98: main (counter.c:105)
==17711==
==17711== Conditional jump or move depends on uninitialised value(s)
==17711==    at 0x400BDB: main (counter.c:107)
==17711==
==17711== Conditional jump or move depends on uninitialised value(s)
==17711==    at 0x400C1B: main (counter.c:109)
==17711==
error
==17711==
==17711== HEAP SUMMARY:
==17711==     in use at exit: 64 bytes in 4 blocks
==17711==   total heap usage: 4 allocs, 0 frees, 64 bytes allocated
==17711==
==17711== LEAK SUMMARY:
==17711==    definitely lost: 0 bytes in 0 blocks
==17711==    indirectly lost: 0 bytes in 0 blocks
==17711==      possibly lost: 0 bytes in 0 blocks
==17711==    still reachable: 64 bytes in 4 blocks
==17711==         suppressed: 0 bytes in 0 blocks
==17711== Rerun with --leak-check=full to see details of leaked memory
==17711==
==17711== For counts of detected and suppressed errors, rerun with: -v
==17711== Use --track-origins=yes to see where uninitialised values come from
==17711== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 6 from 6)
make: *** [test_counter] Error 1

I have a memory leak in this program... i am pretty sure i already free all my dynamic memory.. I don't understand where the leak is =\

#include "counter.h"

/* ============================================================================
 * File-global variables
 * ========================================================================== */
static int ncounters = 0;
static struct counter *counters = NULL;

static int nthreads = 0;
static int *ninstructions = NULL;
static struct instruction **instructions = NULL;


/* ============================================================================
 * Operations
 * ========================================================================== */
static void
decrement(long long *n) {
        (*n)--;
}

static void
increment(long long *n) {
    (*n)++;
}

static void
mult2(long long *n) {
    (*n)=(*n)*2;
}


/* ============================================================================
 * Helper functions
 * ========================================================================== */

pthread_mutex_t lock1;

/* ============================================================================
 * Thread function
 * ========================================================================== */
static void *
worker_thread(void *arg) {

        pthread_mutex_lock( &lock1 );

        long long n = (long long)arg;
        for(int i=0; i < ninstructions[n] ;i++){
          for(int j=0; j < instructions[n][i].repetitions ;j++){
            (*instructions[n][i].work_fn)(&((instructions[n][i].counter)->counter));
          }
        }
        pthread_mutex_unlock( &lock1 );
    return NULL;
}


/* ============================================================================
 * Main function
 * ========================================================================== */
int
main(void) {

        scanf(" %d", &ncounters); //Ask for number of counters

        int i;

        if((counters = malloc(sizeof(struct counter)*ncounters))){
          for( i=0; i < ncounters ;i++){
            counters[i].counter = 0;
          }
        }

        scanf(" %d", &nthreads); //Ask for number of threads

        if((ninstructions = (int*) malloc(nthreads*sizeof(int)))){
          for( i=0; i < nthreads ;i++){
            ninstructions[i] = 0;
          }
        }

        instructions = malloc(nthreads*sizeof(struct instruction *));

        for(i=0; i < nthreads ;i++){

          scanf(" %d", &ninstructions[i]);  //Ask for number of instructions within threads[i]

          instructions[i] = malloc(ninstructions[i]*sizeof(struct instruction));
          for(int j=0;j < ninstructions[i] ;j++){
            int Srepetition;
            char Sfunction;
            int Scounter;

            scanf(" %d %c %d", &Scounter, &Sfunction, &Srepetition);

            instructions[i][j].repetitions = Srepetition;

            instructions[i][j].counter = (counters+Scounter);

            if(Sfunction == 'I'){
              instructions[i][j].work_fn = increment;
            }else if(Sfunction == 'D'){
              instructions[i][j].work_fn = decrement;
            }else if(Sfunction == '2'){
              instructions[i][j].work_fn = mult2;
            }else{
              printf("error\n");
            }

          }

        }

        pthread_t thread_id[nthreads];

        for(long long i=0; i < nthreads ;i++){
          pthread_create( &thread_id[i], NULL, worker_thread, (void *)i);
        }

        for(int i=0; i < nthreads ;i++){
          pthread_join( thread_id[i], NULL);
        }

        for(i=0; i < ncounters ;i++){
          printf("%lld\n", counters[i].counter);
        }

        for(int i=0; i< nthreads;i++){
          free(instructions[i]);
        }
        free(instructions);
        free(ninstructions);
        free(counters);
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过气美图社 2024-11-11 07:07:46

您释放指令占用的内存:

free(instructions); 

然后您访问该内存:

for(int i=0 ; i < nthreads ; i++){
    free(instructions[i]); 
} 

释放(指令)后您无法访问指令

您需要首先释放instructions中的指针,然后释放instructions数组。

You free the memory that instructions occupies:

free(instructions); 

Then you access that memory:

for(int i=0 ; i < nthreads ; i++){
    free(instructions[i]); 
} 

You cannot access instructions after you free(instructions).

You need to free the pointers in instructions first, then free the instructions array.

晚雾 2024-11-11 07:07:46

请添加选项 --leak-check=full 以及 --show-reachable=yes 到 Valgrind 并将结果与​​当前源代码一起发布。当然,针对可执行文件的调试版本运行 Valgrind。

Please add option --leak-check=full and maybe --show-reachable=yes to Valgrind and post result together with current source code. Of course run Valgrind against debug version of your executable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文