[zeromq-dev] ZeroMQ Cleaning up?

Riskybiz riskybizLive at live.com
Fri Jul 19 13:46:24 CEST 2013


So I've built a C++ DLL to integrate with a third party application on
Windows 7 using Visual Studio 2012.  The DLL has two functions to manage a
ZeroMQ `hello world` server (from the ZeroMQ guide document).  One function
to CREATE and launch the server in a separate thread, the other DESTROY
function to interrupt the server and terminate the thread.

 

All goes well, the server is started by CREATE and I can communicate with it
using 'hello world' client (again from the ZeroMQ guide document).  The
problem I need to solve appears after DESTROY is called:

 

When DESTROY is called all seems well initially, the third party application
remains responsive; however if I run hello world client again (in the
knowledge that the server should have gone!) then it causes the third party
application to crash.

 

This makes me suspect that the ZeroMQ resources are not being
released/destroyed properly, this is further complicated by uncertainty as
to whether the error thrown by boost::this_thread::interruption_point() is
being caught or not and consequently whether my ZeroMQ clean up routine is
called (I don't get any output from the catch block to confirm this).  I've
posted to the Boost mailing list also on this subject.

 

Question is: this is a simple interpretation of the 'hello world' server in
a DLL; should it be necessary to carry out additional measures to clean up
resources such as:

 

socket.close();//Close socket for orderly exit

zmq_ctx_destroy(context);//When you exit the program, close your sockets and
then call zmq_ctx_destroy(). This destroys the context.

 

....given that these clean up instructions are not necessary in the ZeroMQ
exe implementations of `hello world` server.

 

Have I correctly implemented the instructions to create and terminate the
ZeroMQ sockets & communications in this code?  Any suggestions for a way
around this problem?

 

With thanks,

 

Riskybiz.

 

 

static bool createCalled = false;//initialise static variable

static bool destroyCalled = false;//initialise static variable

static boost::thread *serverThread = nullptr;

 

int __stdcall CREATE()

{

if(createCalled == false)

{

createCalled = true;

OutputDebugStringA("TestDataAccess: CREATE");

serverThread = new boost::thread(ListenOnReplySocket);//create instance of
boost::thread object

return 0;

}

return 1;

}

 

 

int __stdcall DESTROY()

{

if(createCalled == true && destroyCalled == false)

{

destroyCalled = true;

OutputDebugStringA("TestDataAccess: DESTROY");

serverThread->interrupt();//set flag for interruption

delete serverThread;

return 0;

}

return 1;

}

 

Server code:

 

#ifndef ZMQ_COMMUNICATIONS_H//if not defined already

#define ZMQ_COMMUNICATIONS_H//then define it

 

#define _WINSOCK2API_ //stops windows.h including winsock2.h

 

#include <zmq.hpp>

#include "boost\thread.hpp"

#include <stdexcept>

 

void ListenOnReplySocket()

{

//  Prepare our context and socket

    zmq::context_t context (1);

    zmq::socket_t socket (context, ZMQ_REP);

    socket.bind ("tcp://*:5555");

 

//  Configure socket to not wait at close time

    int linger = 0;

    socket.setsockopt (ZMQ_LINGER, &linger, sizeof (linger));

 

//Enable thread interruption

boost::this_thread::interruption_enabled();

 

try

{

while (true)

{

        zmq::message_t request;

 

        //  Wait for next request from client

        socket.recv (&request);

 

char buffer[50];

int j;

j = sprintf_s(buffer, 50, "TestDataAccess: ZMQComms: Hello");

OutputDebugStringA(buffer);

       

 

        //  Do some 'work'

        Sleep (1000);

 

        //  Send reply back to client

        zmq::message_t reply (5);

        memcpy ((void *) reply.data (), "World", 5);

        socket.send (reply);

 

boost::this_thread::interruption_point();//check iterruption flag

 

}

 

}

catch(boost::thread_interrupted &bti)

{

socket.close();//Close socket for orderly exit

zmq_ctx_destroy(context);//When you exit the program, close your sockets and
then call zmq_ctx_destroy(). This destroys the context.

 

char buffer1[100];

int s;

s = sprintf_s(buffer1, 100, "TestDataAccess: ZMQComms: Server Thread
Interrupted");

OutputDebugStringA(buffer1);

}

}

#endif

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20130719/5c609c36/attachment.htm>


More information about the zeromq-dev mailing list