Hm... I'll look into it, but allocating `uv_write_t` on-stack is definitely
a bad idea. You should
probably try to embed it or allocate it on heap.

However, `uv_try_write` isn't using it, so the error should be somewhere
else...


On Fri, Jun 6, 2014 at 10:10 AM, Jason W <[email protected]> wrote:

> Thanks Fedor.
>
> Sorry not sure how to properly get that in xcode, but here is the line
> that xcode shows when EXC_BAD_ACCESS occurs.
>
>       w->cb(loop, w, revents);
>
> The thread stack is like the following
>
> Thread 1
>
>  Queue: com.apple.main-thread
>
>  0_mh_execute_header
>
>  1 uv_io_poll
>
>  2 uv_run
>
>  3 main
> Below is my 'minimal' code and the line that causes the problem is
> uv_try_write within the broadcastMsg function.  If I comment that out, no
> problem.  Thank you again.
>
> static QUEUE* q;
>
> static QUEUE queue;
> typedef struct
> {
>     uv_tcp_t handle;
>     uv_shutdown_t shutdown_req;
>     QUEUE node;
>
> } client;
>
> void on_write(uv_write_t* req, int status)
> {
>     if (status) {
> //        uv_err_t err = uv_last_error(loop);
>         fprintf(stderr, "uv_write error:\n");
>     return;
>     }
>     wroteCounter++;
>   printf("wrote %d .\n", wroteCounter);
> }
>
> static void broadcastMsg(char *phoneNumber) {
>     q = QUEUE_HEAD(&queue);
>
>
>     QUEUE_FOREACH(q, &queue) {
>         client* user;
>         user = QUEUE_DATA(q, client, node);
>
>         uv_stream_t* theStrPoint = (uv_stream_t*) &(user->handle);
>         char myBuf[12];
>
>         uv_buf_t buf = { .base = strcat(myBuf, "\n"), .len = strlen(myBuf)
> + 1 };
>         int nbufs = 1;
>
>         int byteWritten = uv_try_write(theStrPoint,
>                      &buf,
>                      nbufs);
>
> //        uv_stream_t* stream = (uv_stream_t*) &user->handle;
> //        uv_buf_t buffer[] = {
> //            {.base = "hello", .len = 5},
> //            {.base = "world\n", .len = 6}
> //        };
> //
> //        uv_write_t request;
> //uv_write(&request, stream, &buf, nbufs, on_write);
>
>     }
> }
>
>
> static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t*
> buf) {
>     client* conn;
>     int r;
>
>     if (nread >= 0) {
> ...
>         return;
>     }
>
>
>     ASSERT(nread == UV_EOF);
>     conn = container_of(stream, client, handle);
>
>     r = uv_shutdown(&conn->shutdown_req, stream, shutdown_cb);
>     ASSERT(r == 0);
> }
>
> static void on_connection(uv_stream_t* stream, int status) {
>     int r;
>
>     ASSERT(status == 0);
>     ASSERT(stream == (uv_stream_t*)&tcp_server);
>
>
>     client *client = malloc(sizeof(*client));
>
>
>     r = uv_tcp_init(stream->loop, &client->handle);
>
>     uv_tcp_keepalive(&client->handle, 1, 5);
>
>
>     r = uv_accept(stream, (uv_stream_t*)&client->handle);
>
>     QUEUE_INIT(&client->node);
>
>     QUEUE_INSERT_TAIL(&queue, &client->node);
>
>     r = uv_read_start((uv_stream_t*)&client->handle, alloc_cb, read_cb);
>      ASSERT(r == 0);
> }
>
>
> static void alloc_cb(uv_handle_t* handle,
>                      size_t suggested_size,
>                      uv_buf_t* buf) {
>     //static char slab[65536];
>     static char slab[512];
>     buf->base = slab;
>     buf->len = sizeof(slab);
> }
>
> void alloc_fs_cb(uv_handle_t* handle,
>                      size_t suggested_size,
>                      uv_buf_t* buf) {
>     static char slab[65536];
>     buf->base = slab;
>     buf->len = sizeof(slab);
> }
>
>
> void read_stdin(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
>     if (nread >= 0) {
>         broadcastMsg("test");
>         return;
>
>     }
>
>
>     ASSERT(nread == UV_EOF);
>     ...
> }
>
> void setupus()
> {
>
>
>     uv_fs_t file_req;
>     uv_pipe_t file_pipe;
>
>     int fd = uv_fs_open(loop, &file_req, "path-to-usb", O_RDONLY, 0, NULL);
>
>     uv_pipe_init(loop, &file_pipe, 0);
>     uv_pipe_open(&file_pipe, fd);
>
>
>     uv_read_start((uv_stream_t*)&file_pipe, alloc_fs_cb, read_stdin);
> }
>
> int main(void) {
>     struct sockaddr_in addr;
>     int r;
>
>     QUEUE_INIT(&queue);
>
>     loop = uv_default_loop();
>     r = uv_ip4_addr(SERVER_ADDR, SERVER_PORT, &addr);
>
>     r = uv_tcp_init(loop, &tcp_server);
>     uv_tcp_keepalive(&tcp_server, 1, 5);
>
>     r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
>
>     r = uv_listen((uv_stream_t*)&tcp_server, 128, on_connection);
>
>
>     setupus();
>
>     r = uv_run(loop, UV_RUN_DEFAULT);
>
>     return 0;
> }
>
>
> On Thursday, June 5, 2014 11:50:40 PM UTC-4, Fedor Indutny wrote:
>
>> Hello Jason,
>>
>> I'm afraid it is quite hard to say, what exactly is happening here
>> without taking a look at the surrounding code.
>>
>> Could you please try to obtain a stack trace of the EXC_BAD_ACCESS that
>> you are getting?
>>
>> Generally, one loop should be enough for all purposes!
>>
>> Cheers,
>> Fedor.
>>
>>
>> On Thu, Jun 5, 2014 at 11:11 PM, Jason W <[email protected]> wrote:
>>
>>> Hello, I develop a small program that has the following two things.
>>>
>>> 1. tcp server that and uses a queue to queue up client connections
>>>
>>> 2. make use of pipe to read events from usb
>>>
>>>
>>> Once an event is triggered by usb, the idea is to notify all the tcp
>>> clients in the queue.  The connected tcp clients can receive the
>>> message being published by the server with the following code on the
>>> server side BUT my on_write function just keep getting called like
>>> inifite loop.
>>>
>>>
>>> uv_write(&request, stream, &buf, nbufs, on_write);
>>>
>>>
>>> However if I change the above code w/ the following, I will get error
>>> Thread 1:EXC_BAD_ACCESS (code=EX_i386_GPFLT)
>>>
>>>
>>>
>>>         int byteWritten = uv_try_write(theStrPoint, &buf, nbufs);
>>>
>>>
>>>
>>> I only use the default loop and wonder whether I should create another
>>> loop for the above 2 pipe thing?
>>>
>>> My env is Mac using xcode and libuv version 0.11.25, could that also be
>>> the unstable thing?  Thank you.
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "libuv" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>>
>>> Visit this group at http://groups.google.com/group/libuv.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"libuv" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.

Reply via email to