libzmq: Visual Studio 2012 x64 Unhandled Exception Error

With the latest stable release 4.2.1, I’m having an issue where the creation and deletion of a socket causes a crash in Visual Studio 2012 64 bit builds. I’ve tried building the library from the provided msvc projects and using CMake and get the same result. This does not happen in later versions of VS that I have tested (2015), and also does not occur in 32 bit builds in VS2012. I’ve pasted sample code below, the program will crash with an unhandled exception error inside of the libzmq library, in a thread the socket or context creates when using a map on line 77 of select.cpp. I’ve included sample code that demonstrates this. This occurs when running in both debug and release under x64.

#include <zmq.h>

int main()
{
    zmq_msg_t msg;
    void* context = zmq_ctx_new();
    void* socket = zmq_socket(context, ZMQ_SUB);

    if(socket)
    {
        zmq_close(socket);
    }

    if(context)
    {
        zmq_ctx_destroy(context);
    }

    return 0;
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 27 (27 by maintainers)

Most upvoted comments

This issue is also seen with VS2010 Win64, where std::map is presumably having the same issue, though the code is slightly different.

While I am not quite sure the following workaround deserves a PR (due to introducing an extra dependency), I will put it here for those who may be hitting this too (for example, being in the same situation as me: VS2010 Win64, no chance to increase stack size since what I do is a DLL and setting stack size is only done at process level, which is out of my control):

WORKAROUND:

boost::container::map seems to be a drop-in replacement for std::map, therefore, if you already use Boost, or at least have it installed and are allowed to use it in your project, just use something like this:

diff --git a/src/select.hpp b/src/select.hpp
index e76ebcaa..dce8c7f5 100644
--- a/src/select.hpp
+++ b/src/select.hpp
@@ -36,7 +36,8 @@
 
 #include <stddef.h>
 #include <vector>
-#include <map>
+// #include <map>
+#include "boost/container/map.hpp"
 
 #if defined ZMQ_HAVE_WINDOWS
 #elif defined ZMQ_HAVE_OPENVMS
@@ -128,7 +129,8 @@ class select_t : public poller_base_t
                               struct timeval &tv_);
 
 #if defined ZMQ_HAVE_WINDOWS
-    typedef std::map<u_short, family_entry_t> family_entries_t;
+    // typedef std::map<u_short, family_entry_t> family_entries_t;
+    typedef boost::container::map<u_short, family_entry_t> family_entries_t;
 
     struct wsa_events_t
     { 

Cheers.

Sergey.

296 bytes. That means std::map is fixed in VS 2013. Great, thank you! I’ll make a PR.