c - 使用内存泄漏的 pthread

标签 c pthreads pthread-join

只是一个测试多线程的小程序。它应该打印出带有线程索引和线程位置的“Hello”消息。

我读了why pthread cause memory leak并尝试使用 pthread_join。看来内存泄漏还是存在

以下是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>

void runMe(int *arg) {
    printf("Hello %d from %x\n", *arg, (unsigned int)pthread_self());
    int *ret = (int*)malloc(sizeof(int));
    *ret = *arg + 4; // note this value '4' will be different in the quiz!

    pthread_exit((void*)ret);
}

int run_threads(int n) {
    pthread_t thr[n];
    int thr_args[n];
    int *ptr = 0;
    int total = 0;

    for (int i=0; i<n; i++) {
        thr_args[i] = i;
        pthread_create(&thr[i], NULL, (void*)runMe, &thr_args[i]);

    }
    for (int j=0; j<n; j++) {
        pthread_join(thr[j], (void*)ptr++);
        total += thr_args[j];
    }
    return total;
}

int main() {

    run_threads(10);

}

以下是运行valgrind的结果:

==10292== 
==10292== HEAP SUMMARY:
==10292==     in use at exit: 1,654 bytes in 14 blocks
==10292==   total heap usage: 26 allocs, 12 frees, 5,454 bytes allocated
==10292== 
==10292== LEAK SUMMARY:
==10292==    definitely lost: 40 bytes in 10 blocks
==10292==    indirectly lost: 0 bytes in 0 blocks
==10292==      possibly lost: 0 bytes in 0 blocks
==10292==    still reachable: 1,614 bytes in 4 blocks
==10292==         suppressed: 0 bytes in 0 blocks
==10292== Rerun with --leak-check=full to see details of leaked memory
==10292== 
==10292== For counts of detected and suppressed errors, rerun with: -v
==10292== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

最佳答案

你的泄漏是因为你分配的内存没有对应的free action。

您正在使用的代码似乎试图将动态分配传回给调用者。正确使用 pthread_join 及其第二个参数可以回收该内存指针,然后可以正确释放它。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>

void* runMe(void *pv)
{
    int *arg = pv;
    printf("Hello %d from %x\n", *arg, (unsigned int)pthread_self());
    int *ret = malloc( sizeof *ret );
    *ret = *arg + 4; // note this value '4' will be different in the quiz!
    return ret;
}

int run_threads(int n) {
    pthread_t thr[n];
    int thr_args[n];
    int total = 0;

    for (int i=0; i<n; i++) {
        thr_args[i] = i;
        pthread_create(thr+i, NULL, runMe, thr_args+i);

    }
    for (int j=0; j<n; j++)
    {
        // reap pointer from resulting thread.
        void *res = NULL;
        pthread_join(thr[j], &res);

        int *ires = res;
        printf("ires = %p; *ires = %d\n", ires, *ires);
        free(ires);

        total += thr_args[j];
    }
    return total;
}

int main()
{
    run_threads(10);
}

输出(变化)

Hello 9 from b0a71000
Hello 0 from b05df000
Hello 7 from b096d000
Hello 2 from b06e3000
Hello 1 from b0661000
Hello 4 from b07e7000
Hello 3 from b0765000
Hello 5 from b0869000
Hello 6 from b08eb000
Hello 8 from b09ef000
ires = 0x600000; *ires = 4
ires = 0x2009e0; *ires = 5
ires = 0x600010; *ires = 6
ires = 0x500010; *ires = 7
ires = 0x2009f0; *ires = 8
ires = 0x2012c0; *ires = 9
ires = 0x600020; *ires = 10
ires = 0x400040; *ires = 11
ires = 0x400050; *ires = 12
ires = 0x500000; *ires = 13

我还冒昧地修复了您使用的不正确、不合规的函数签名。 pthread_create 需要形式:

void *proc(void *)

为线程程序。任何不属于这种形式的行为都是不合规的,应该避免。

关于c - 使用内存泄漏的 pthread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45518996/

相关文章:

c - 关于递归的问题

C# DLL 的包装器

c - 如果不等于上一行则打印每一行

c++ - 我必须运行 cmake 两次才能编译项目

c - 使用 2 个线程对 2 个数组进行排序比将 2 个数组一一排序需要更多时间

c - fork多线程进程,为线程分配内存

函数内的 Char 指针分配 - 段错误

C - pthread 函数重用 - 局部变量和竞争条件

c - 如何计算正在运行的线程数 (pthreads)?

在 C 中创建没有 pthread_join 的线程