stephenprocter

Possible memory leak in zmq::msg_t

Apr 3rd, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. #include "czmq.h"
  2.  
  3. int main(int argc, char* argv[])
  4. {
  5.     const char* endpoint = "inproc://test";
  6.     const char* msg = "Hello Hello Hello Hello Hello!";  // length 30
  7.  
  8.     // create context
  9.     zctx_t* ctx = zctx_new();
  10.     assert(ctx);
  11.  
  12.     // create and bind pull socket
  13.     void* pull = zsocket_new(ctx, ZMQ_PULL);
  14.     int port = zsocket_bind(pull, endpoint);
  15.  
  16.     // create and connect push socket
  17.     void* push = zsocket_new(ctx, ZMQ_PUSH);
  18.     int rc = zsocket_connect(push, endpoint);
  19.     assert(rc == 0);
  20.  
  21.     // destroy puller, then send
  22.     zsocket_destroy(ctx, pull);
  23.     sleep(1);
  24.     rc = zsocket_sendmem(push, msg, strlen(msg), ZFRAME_DONTWAIT);
  25.     assert(rc == -1);
  26.  
  27.     // clean up
  28.     zsocket_destroy(ctx, push);
  29.     zctx_destroy(&ctx);
  30. }
  31.  
  32. /*
  33. Output:
  34.  
  35. # valgrind --leak-check=full --show-reachable=yes ./testcase
  36. ==4354== Memcheck, a memory error detector
  37. ==4354== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
  38. ==4354== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
  39. ==4354== Command: ./testcase
  40. ==4354==
  41. ==4354==
  42. ==4354== HEAP SUMMARY:
  43. ==4354==     in use at exit: 70 bytes in 1 blocks
  44. ==4354==   total heap usage: 66 allocs, 65 frees, 40,842 bytes allocated
  45. ==4354==
  46. ==4354== 70 bytes in 1 blocks are definitely lost in loss record 1 of 1
  47. ==4354==    at 0x4C277AB: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
  48. ==4354==    by 0x5098850: zmq::msg_t::init_size(unsigned long) (msg.cpp:60)
  49. ==4354==    by 0x4E5212B: zsocket_sendmem (zsocket.c:211)
  50. ==4354==    by 0x400B19: main (in /root/zeromq/testcase)
  51. ==4354==
  52. ==4354== LEAK SUMMARY:
  53. ==4354==    definitely lost: 70 bytes in 1 blocks
  54. ==4354==    indirectly lost: 0 bytes in 0 blocks
  55. ==4354==      possibly lost: 0 bytes in 0 blocks
  56. ==4354==    still reachable: 0 bytes in 0 blocks
  57. ==4354==         suppressed: 0 bytes in 0 blocks
  58. ==4354==
  59. ==4354== For counts of detected and suppressed errors, rerun with: -v
  60. ==4354== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
  61. */
Advertisement
Add Comment
Please, Sign In to add comment