There is a void pointer in the epoll event structure. I am malloc-ing memory for this and using it elsewhere. Now when I want to take the FD off the epoll list, will this pointer be freed by itself? (A logical thought chain would be that the event structure is maintained internally, possibly by the kernel. So it would free the event strucutre and the memory allocated to the pointer too) Or would I have to free it explicitly?
P.S. I saw a piece of code that does not free the memory allocated to the pointer. I feel that it should be freed explicitly.
No, the pointer will not be freed by itself. As far as the kernel is concerned, it’s just an opaque value which it doesn’t know how to deal with. It doesn’t know if it’s a pointer into the stack, heap, data segment, or something else entirely. So it couldn’t possibly know that it should
free()it.The basic rule is, unless the documentation explicitly says otherwise, you are responsible for
freeing any memory youmalloc. So, when you remove a file descriptor from an epoll list, you also need tofreethe corresponding pointer youmalloced.