Why ZeroMQ poll throws an error: "socket operation on non-socket"?

Why ZeroMQ poll throws an error: "socket operation on non-socket"?

我遇到了一个关于 ZeroMQ 及其 C++ 绑定的严重而奇怪的问题:每次我在 zmq 套接字上执行轮询操作时,它都会抛出一个错误:

socket operation on non-socket

下面是重现它的代码:

#include <zmq.hpp>
int main() {

  zmq::context_t       ctx(1);
  zmq::socket_t sock1( ctx, ZMQ_REQ );
  zmq::socket_t sock2( ctx, ZMQ_PUB );

  sock1.bind( "tcp://*:10001" );
  sock2.bind( "tcp://*:10002" );

  zmq::pollitem_t* items = ( zmq::pollitem_t* ) malloc( 2 * sizeof( zmq::pollitem_t ) );

  items[0] = { &sock1, 0, ZMQ_POLLIN, 0 };
  items[1] = { &sock2, 0, ZMQ_POLLIN, 0 };

  while(1) {
    zmq::poll ( items, 2, 500 );  // Throws error !! socket operation on non-socket.
  }
}

鉴于 ZeroMQ API / C++ 绑定文档建议这样做,应该遵循以下做法:

To obtain a ØMQ socket for use in a zmq_pollitem_t structure, you should cast an instance of the socket_t class to (void *).