valgrind memory leak errors when using pthread_create

后端 未结 5 1065
离开以前
离开以前 2020-11-30 06:58

I\'m writing a program using the pthread library. When I run my program with the command valgrind --leak-check=full, I get the following errors description:

5条回答
  •  死守一世寂寞
    2020-11-30 07:23

    You can make the thread in detached state to avoid the memory leak if the thread should not be joined (or just expires on it's own).

    To explicitly create a thread as joinable or detached, the attr argument in the pthread_create() routine is used. The typical 4 step process is:

    • Declare a pthread attribute variable of the pthread_attr_t data type
    • Initialize the attribute variable with pthread_attr_init()
    • Set the attribute detached status with pthread_attr_setdetachstate()
    • When done, free library resources used by the attribute with pthread_attr_destroy()

提交回复
热议问题