title: Swift memcpy doesn't have any effect when used with ZMQ zmq_msg_data
body: <p>I've been trying to write a libzmq wrapper for Swift by building off of an existing wrapper called <a href="https://github.com/appliedengineering/SwiftyZeroMQ5" rel="nofollow noreferrer">SwiftyZeroMQ</a>. However, for our purposes, we require the usage of raw UDP which means we need to use ZeroMQ's <a href="https://rfc.zeromq.org/spec/48/" rel="nofollow noreferrer">Radio/Dish draft method</a>.</p>
  <p>I've been able to successfully write a wrapper for receiving data via the Dish socket but I'm now trying to write a wrapper for sending data via the Radio socket. There doesn't seem to be any stuff online regarding how to write a function to send data via the Radio socket but I did come across <a href="https://github.com/zeromq/libzmq/blob/master/tests/test_radio_dish.cpp#L56" rel="nofollow noreferrer">this</a>. This function in the libzmq repo tests the sending of data via the radio socket so I figured why not try and replicate the function in Swift.</p>
  <p>Here's what I've come up with:</p>
  <p>Inside the <a href="https://github.com/appliedengineering/SwiftyZeroMQ5/blob/master/Sources/Socket.swift" rel="nofollow noreferrer">Socket.swift</a> file:</p>
  <pre><code>     public func sendRadioMessage(_ group: String, data: NSData) throws{
              var msg = zmq_msg_t.init();
              var result : Int32;
              let flags: SocketSendRecvOption = .none
              
              
              result = zmq_msg_init(&amp;msg);
              if (result == -1) { throw ZeroMQError.last }
          
              defer {
                  // Clean up message on scope exit
                  zmq_msg_close(&amp;msg)
              }
              
              print(&quot;initial msg data = \(zmq_msg_data(&amp;msg))&quot;)
              
              print(&quot;initial data = \(data)&quot;)
              memcpy(zmq_msg_data(&amp;msg), (data as NSData).bytes, data.length);
              
              print(&quot;msg size = \(zmq_msg_size(&amp;msg))&quot;)
              print(&quot;msg = \(zmq_msg_data(&amp;msg))&quot;)
              
              result = zmq_msg_set_group(&amp;msg, group);
              if (result == -1) { throw ZeroMQError.last }
              
              result = zmq_msg_send(&amp;msg, self.handle, flags.rawValue);
              if (result == -1) { throw ZeroMQError.last }
              print(&quot;sent \(result) bytes&quot;)
          }
          
      }
  </code></pre>
  <p>and that function is then called like this:</p>
  <pre><code>    public func send(data: String) -&gt; Bool{
          do{
              try radio?.sendRadioMessage(group, data: data.data(using: .utf8) as! NSData);
          }
          catch{
              print(&quot;SEND COMMUNICATION error - \(error)&quot;);
              return false;
          }
          return true;
      }
  
     obj.send(data: &quot;Test Data&quot;)
  </code></pre>
  <p>This is the console output when running the program:</p>
  <p><a href="https://i.stack.imgur.com/zN5H4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zN5H4.png" alt="Console output" /></a></p>
  <p>It seems to me then that the <code>memcpy</code> function that I'm calling from Swift doesn't actually seem to be doing anything. Would this be due to the fact that I'm not passing the data payload properly to the <code>memcpy</code> function? I'm a bit stuck.</p>