英文:
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 < num; i++)
{
parameters *p = malloc(sizeof(parameters));
p-> something = i;
pthread_create(&tid[i], NULL, (void *)threadFunc, p);
}
// some heavy main logic
for (int i = 0; i < 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 <source-goes-here> -lptread
$ valgrind --trace-children=yes --child-silent-after-fork=yes --leak-check=full --show-leak-kinds=all -s <compiled-program-here>
- Modified code to compile (with assumptions)
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
// 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 < num; i++)
{
parameters *p = malloc(sizeof(parameters));
p-> something = i;
pthread_create(&tid[i], NULL, (void *)threadFunc, p);
}
// some heavy main logic
for (int i = 0; i < 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论