[zeromq-dev] How should sockets be destroyed?

Olaf Mandel o.mandel at menlosystems.com
Mon Feb 24 12:12:40 CET 2014


Am 24.02.2014 11:43, schrieb Laurent Alebarde:
> Where is your socket variable declaration ?
-Snipp-
> 
> Le 24/02/2014 11:32, Olaf Mandel a écrit :
-Snipp-
>>
>> int const l = 0;
>> int i;
>> for(i=0; i<10000; ++i) {
>>      socket = zmq_socket(context, ZMQ_REP);
>>      rc = zmq_setsockopt(socket, ZMQ_LINGER, &l, sizeof(l)); /* #1 */
>>      rc = zmq_connect(socket, "inproc://demo");
>>      rc = zmq_close(socket);
>> }
>>
-Snipp-

Hello Laurent,

the socket is declared outside of the loop. Here is the full code, again
without assertions and debug output:

#include <zmq.h>

int main(void)
{
    int const l = 0;

    void* context;
    void* socket;
    int   rc;
    int   i;

    context = zmq_ctx_new();
    for(i=0; i<10000; ++i) {
        socket = zmq_socket(context, ZMQ_REP);
        rc = zmq_setsockopt(socket, ZMQ_LINGER, &l, sizeof(l));
        rc = zmq_connect(socket, "inproc://demo");
        rc = zmq_close(socket);
    }

    rc = zmq_ctx_destroy(context);
    return 0;
}


> You shall use one variable per socket, so, use an array.

After the zmq_close() call, I was expecting it to be ok to reuse the
socket variable, same like you may reuse a pointer-variable after
deleting the original contents. And if there really is a need to keep a
reference to the socket after the zmq_close() call, that would actually
be my original question: how to properly close a socket to that it is
really closed?

Best regards,
Olaf Mandel


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20140224/facab5dd/attachment.sig>


More information about the zeromq-dev mailing list