On Wed, Apr 6, 2016 at 3:22 PM, Kaushik S <[email protected]> wrote:
> I'm writing a simple application that makes use of libuv event loop to do
> asynchronous event handling.
>
> Following is my program
>
>     // includes
>
>
>     uv_fs_t open_req;
>     uv_loop_t * my_default_loop;
>     uv_poll_t * file_poll;
>
>     void read_callback( uv_poll_t * handle, int status, int events)
>     {
>         if(!status)
>             printf("There is some data for reading");
>         else
>             printf("nothing to read");
>     }
>
>     int main() {
>
>         int fd;
>
>         file_poll = (uv_poll_t *)malloc(sizeof(uv_poll_t));
>
>         my_default_loop = uv_default_loop();
>         uv_loop_init(my_default_loop);
>         my_default_loop->data = NULL;
>
>         fd=open("poll.txt", O_RDONLY);
>         printf("FD : %d",fd);
>         uv_run(my_default_loop, UV_RUN_DEFAULT);
>
>         uv_poll_init(my_default_loop, file_poll, fd);
>         uv_poll_start(file_poll, UV_READABLE, read_callback);
>
>         uv_fs_req_cleanup(&open_req);
>
>         return 0;
>     }
>
>
>
> I followed the API documentation on uv_poll_t[1] - Poll Handle and wrote
> this demo program.
>
> My demo program tries to monitor a file called poll.txt. And should output
> if there is any data to be read.
>
> This is the error I get when I execute my program
>
>     [1]    18320 segmentation fault  ./test
>
>
>
> Here's the GDB output when I run the program
>
>     Program received signal SIGSEGV, Segmentation fault.
>     uv__io_stop (loop=0x0, w=w@entry=0x61d078, events=events@entry=8197) at
> ../src/unix/core.c:878
>     878       if ((unsigned) w->fd >= loop->nwatchers)
>
>
>
> Here's the GDB Backtrace
>
>     #0  uv__io_stop (loop=0x0, w=w@entry=0x61d078, events=events@entry=8197)
> at ../src/unix/core.c:878
>     #1  0x000000000040b0b7 in uv__poll_stop (handle=0x61d010) at
> ../src/unix/poll.c:80
>     #2  uv_poll_start (handle=0x61d010, pevents=1, poll_cb=0x4038d0
> <read_callback>) at ../src/unix/poll.c:100
>     #3  0x00000000004039c0 in main () at test.c:33
>
>
> Can any one point me where I am doing wrong with the setup and usage ?
>
>
>
>   [1]: http://docs.libuv.org/en/v1.x/poll.html

There are a couple of things wrong with your example.

>         my_default_loop = uv_default_loop();
>         uv_loop_init(my_default_loop);

The default loop is already initialized.  You're overwriting its contents.

>         uv_fs_req_cleanup(&open_req);

open_req hasn't been initialized.

-- 
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 https://groups.google.com/group/libuv.
For more options, visit https://groups.google.com/d/optout.

Reply via email to