I have the following struct
typedef struct test {
int action;
void *data;
void (*function)(int, void*);
} test;
int execute_func(void(*function)(int a, void *d), int action, void *data)
{
struct test *todo;
todo = calloc (1,sizeof(struct test));
if (todo == NULL)
{
return -1;
}
todo->action = action;
todo->data = data;
todo->function = function;
todo->function(todo->action, todo->data);
return 0;
}
After executing the function, i want to free the allocated structure usinf the following:
if(todo != NULL)
{
if(todo->data != NULL)
{
free(todo->data);
}
if(todo->function != NULL)
{
free(todo->function); //Cause a crash
}
free(todo);
}
but i get a crash.
See Question&Answers more detail:os