zeromq - Memory leakage in zmq client in c -


memory leakage in zmq client running using valgrind code:

#include<stdio.h> #include<stdlib.h> #include<zmq.h> #include<unistd.h> #include<string.h> #include <signal.h> volatile sig_atomic_t stop; void inthand(int signum) {     stop = 1; } int main(){         signal(sigint, inthand);         char buffer[1024];         int i=0;         void *context = zmq_ctx_new();         void *request = zmq_socket(context, zmq_req);         zmq_connect(request,"tcp://10.30.11.215:2424");         while(!stop){                 memset(buffer,0,1024);                 zmq_send(request,"hello",5,0);                 zmq_recv(request,buffer,sizeof(buffer),0);         }         zmq_close(request);         zmq_ctx_destroy(context); } 

valgrind log:

==19532== heap summary: ==19532==     in use @ exit: 36,678 bytes in 43 blocks ==19532==   total heap usage: 236 allocs, 193 frees, 138,270 bytes allocated ==19532==  ==19532== leak summary: ==19532==    lost: 0 bytes in 0 blocks ==19532==    indirectly lost: 0 bytes in 0 blocks ==19532==      possibly lost: 36,318 bytes in 38 blocks ==19532==    still reachable: 360 bytes in 5 blocks ==19532==         suppressed: 0 bytes in 0 blocks ==19532== rerun --leak-check=full see details of leaked memory 

why possibly lost leakage occur while running? please solve same.

old , zeromq practice carefull wrt resources:

a due care expected handle messages ( , hidden data-structures ):

zmq_msg_t            amessage; zmq_msg_init_size ( &amessage,  1024 ); zmq_msg_data      ( &amessage, "hello", 5 ); ... zmq_msg_close     ( &amessage ); 

ought take place "behave well".

why? read api-specification due care:

if non-compliant use-cases compiled, leaks explicitly warned in:

the zmq_msg_init_data() function shall initialise message object referenced msg represent content referenced buffer located @ address data, size bytes long. no copy of data shall performed , Ømq shall take ownership of supplied buffer.

if provided, deallocation function ffn shall called once data buffer no longer required Ømq, data , hint arguments supplied zmq_msg_init_data().

never access zmq_msg_t members directly, instead use zmq_msg family of functions.

the deallocation function ffn needs thread-safe, since called arbitrary thread.

if deallocation function not provided, allocated memory not freed, , this may cause memory leak.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -