Hi,<br><br>I&#39;m having a problem wrapping the Rabbit MQ C client<br>library (<a href="https://github.com/alanxz/rabbitmq-c">https://github.com/alanxz/rabbitmq-c</a>) using the ffi.  My<br>problem is with the amqp_queue_declare function.  The C<br>
declaration is:<br><br>amqp_queue_declare_ok_t *<br>amqp_queue_declare(amqp_connection_state_t state,<br>                   amqp_channel_t channel,<br>                   amqp_bytes_t queue,<br>                   amqp_boolean_t passive,<br>
                   amqp_boolean_t durable,<br>                   amqp_boolean_t exclusive,<br>                   amqp_boolean_t auto_delete,<br>                   amqp_table_t arguments);<br><br>It seems to run successfully when wrapped to return an opaque<br>
pointer:<br><br>(define _amqp-queue-declare-ok-pointer (_cpointer &#39;QUEUE-DECLARE-OK))<br><br>(define-amqp amqp_queue_declare<br>  (_fun _amqp-connection-state-pointer<br>        _amqp_channel_t _pointer _int _int _int _int _pointer<br>
        -&gt; _amqp-queue-declare-ok-pointer))<br><br>&gt; #&lt;cpointer:QUEUE-DECLARE-OK&gt;<br><br>Wrapping it to return a cstruct pointer fails with a SEGV.<br><br>&gt; SIGSEGV MAPERR si_code 1 fault on addr (nil)<br><br>
The wrapping code for the c structs is as follows:<br><br>(define _size_t _uint32)<br>(define _amqp_channel_t _uint16)<br><br>(define-cstruct _amqp_bytes_t<br>  ([len _size_t]<br>   [bytes _pointer]))<br><br>;; c definition of amqp_bytes_t is:<br>
;; typedef struct amqp_bytes_t_ {<br>;;   size_t len;<br>;;   void *bytes;<br>;; } amqp_bytes_t;<br><br>(define-cstruct _amqp_queue_declare_ok_t<br>  ([queue _amqp_bytes_t]<br>   [message_count _uint32]<br>   [consumer_count _uint32]))<br>
<br>;; typedef struct amqp_queue_declare_ok_t_ {<br>;;   amqp_bytes_t queue;<br>;;   uint32_t message_count;<br>;;   uint32_t consumer_count;<br>;; } amqp_queue_declare_ok_t;<br><br>(define-amqp amqp_queue_declare<br>  (_fun _amqp-connection-state-pointer<br>
        _amqp_channel_t _pointer _int _int _int _int _pointer<br>        -&gt; _amqp_queue_declare_ok_t))<br><br>Any ideas what I&#39;m doing wrong here?<br><br>The full code so far is below:<br><br>#lang racket/base<br><br>
(require ffi/unsafe<br>         ffi/unsafe/define)<br><br>(define lib-rabbit-mq (ffi-lib &quot;librabbitmq&quot;))<br>(define-ffi-definer define-amqp lib-rabbit-mq)<br><br>;; C types<br><br>(define _size_t _uint32)<br>(define _amqp_channel_t _uint16)<br>
<br>(define-cstruct _amqp_bytes_t<br>  ([len _size_t]<br>   [bytes _pointer]))<br><br>(define-cstruct _amqp_queue_declare_ok_t<br>  ([queue _amqp_bytes_t]<br>   [message_count _uint32]<br>   [consumer_count _uint32]))<br>
<br>(define-cstruct _amqp_method_t<br>  ([id _uint32]<br>   [decoded _pointer]))<br><br>(define-cstruct _amqp_rpc_reply_t<br>  ([reply_type _int]<br>   [reply _amqp_method_t]<br>   [library_error _int]))<br><br>(define _amqp-connection-state-pointer (_cpointer &#39;CONNECTION-STATE))<br>
(define _amqp-channel-open_ok-pointer (_cpointer &#39;CHANNEL-OPEN-OK))<br><br>;; C functions<br><br>(define-amqp amqp_error_string (_fun _int -&gt; _string))<br>(define-amqp amqp_get_rpc_reply (_fun _amqp-connection-state-pointer -&gt;<br>
                                      _amqp_rpc_reply_t))<br><br>(define-amqp amqp_new_connection (_fun -&gt; _amqp-connection-state-pointer))<br>(define-amqp amqp_open_socket (_fun _string _int -&gt; _int))<br>(define-amqp amqp_set_sockfd (_fun _amqp-connection-state-pointer<br>
                                   _int -&gt; _int))<br>(define-amqp amqp_login (_fun _amqp-connection-state-pointer<br>                              _string _int _int _int _int _string _string -&gt;<br>                              _amqp_rpc_reply_t))<br>
(define-amqp amqp_channel_open (_fun _amqp-connection-state-pointer<br>                                     _amqp_channel_t -&gt;<br>                                     _amqp-channel-open_ok-pointer))<br><br>;; *** THIS DOES NOT WORK<br>
<br>(define-amqp amqp_queue_declare<br>  (_fun _amqp-connection-state-pointer<br>        _amqp_channel_t _pointer _int _int _int _int _pointer<br>        -&gt; _amqp_queue_declare_ok_t))<br><br>;; *** THIS SEEMS TO WORK<br>

<br>(define _amqp-queue-declare-ok-pointer (_cpointer &#39;QUEUE-DECLARE-OK))<br><br>(define-amqp amqp_queue_declare<br>  (_fun _amqp-connection-state-pointer<br>        _amqp_channel_t _pointer _int _int _int _int _pointer<br>
        -&gt; _amqp-queue-declare-ok-pointer))<br><br>;; main<br><br>(define conn (amqp_new_connection))<br>(define sockfd (amqp_open_socket &quot;localhost&quot; 5672))<br>(when (&lt; sockfd 0) (amqp_error_string (* -1 sockfd)))<br>
(amqp_set_sockfd conn sockfd)<br>(define *AMQP_SASL_METHOD_PLAIN* 0)<br>(define resp (amqp_login conn &quot;/&quot; 0 131072 0<br>                         *AMQP_SASL_METHOD_PLAIN* &quot;user&quot; &quot;pwd&quot;))<br><br>
(amqp_channel_open conn 1)<br>(amqp_queue_declare conn 1 #f 0 0 0 1 #f)<br>