C: 使用pthread_create和结构参数时出现内存问题。

huangapple go评论138阅读模式
英文:

C: memory issue with pthread_create and struct parameter

问题

看起来我有内存泄漏,有人可以告诉我我做错了什么吗?

我注释掉了一些代码以使其更加可读:

int main(int argc, char *argv[])
{
    pthread_t *tid = malloc(sizeof(pthread_t) * num);
    for (int i = 0; i < num; i++)
    {
        parameters *p = malloc(sizeof(parameters));
        p->something = i;
        
        pthread_create(&tid[i], NULL, (void *)threadFunc, p);
    }
    // 一些重要的主要逻辑
    for (int i = 0; i < num; i++)
    {
        pthread_cancel(tid[i]);
        pthread_join(tid[i], NULL);
    }
    free(tid);
    // 结束部分
}
    
void *threadFunc(parameters *params)
{
    // 接管参数
    free(params);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
    while (1)
    {
        // 进行某些操作
    }
}

这是您提供的代码的翻译部分。如有任何其他问题,请随时提出。

英文:

It seems like I do have a memory leak, can somebody tell me what I am doing wrong?

I commented out some code so that it is a little bit more readable:

int main(int argc, char *argv[])
{
    pthread_t *tid = malloc(sizeof(pthread_t) * num);
    for (int i = 0; i &lt; num; i++)
    {
        parameters *p = malloc(sizeof(parameters));
        p-&gt; something = i;
        
        pthread_create(&amp;tid[i], NULL, (void *)threadFunc, p);
    }
    // some heavy main logic
    for (int i = 0; i &lt; num; i++)
    {
        pthread_cancel(tid[i]);
        pthread_join(tid[i], NULL);
    }
    free(tid);
    // End stuff
}
    
void *threadFunc(parameters *params)
{
    // take over params
    free(params);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
    while (1)
    {
        // do magic
    }
}

答案1

得分: 1

以下是翻译好的部分:

"valgrind output" 翻译为 "valgrind 输出:"。

英文:

There is no memory loss in the example provided.

Using the code from the OP and adjusted it with a few assumptions to make it compile. It does not show any memory loss using valgrind.

  • Commands used to compile & memory check
$ gcc &lt;source-goes-here&gt; -lptread

$ valgrind --trace-children=yes --child-silent-after-fork=yes --leak-check=full --show-leak-kinds=all -s &lt;compiled-program-here&gt;
  • Modified code to compile (with assumptions)
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;pthread.h&gt;

// assumptions here:
typedef struct s_parameters { int something; int anotherthing[100][2]; }  parameters;
// end assumption

// added forward definition of function
void *threadFunc(parameters *);

int main(int argc, char *argv[])
{

// assumptions here:
int num=5;
parameters *p=NULL;
// end assumptions

    pthread_t *tid = malloc(sizeof(pthread_t) * num);
    for (int i = 0; i &lt; num; i++)
    {
        parameters *p = malloc(sizeof(parameters));
        p-&gt; something = i;

        pthread_create(&amp;tid[i], NULL, (void *)threadFunc, p);
    }
    // some heavy main logic
    for (int i = 0; i &lt; num; i++)
    {
        pthread_cancel(tid[i]);
        pthread_join(tid[i], NULL);
    }
    free(tid);
    // End stuff
}

void *threadFunc(parameters *params)
{
    // take over params
    free(params);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
    while (1)
    {
        // do magic
    }
}

  • valgrind output:
 HEAP SUMMARY:
     in use at exit: 0 bytes in 0 blocks
   total heap usage: 16 allocs, 16 frees, 7,130 bytes allocated

 All heap blocks were freed -- no leaks are possible

 ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Good luck

huangapple
  • 本文由 发表于 2023年3月9日 20:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定