Whether or How can I use uv_buf_init with std::string?

307 views
Skip to first unread message

zhengfish

unread,
Jan 22, 2014, 4:15:16 AM1/22/14
to li...@googlegroups.com
Hi, libuv team,

In my code, I want to init one buffer with std::string variable's value.
However I found I cannot call uv_buf_init with std::string directly on Windows-7, if I do that, my program will quit libuv loop without any warning.
As a comparison, I can do that on Linux.
My libuv version is v0.10.21.



Here is my code-snippet:

------------------------------------------------------------code-snippet---------------------------------------------------------

// callback for connect
void on_connect ( uv_connect_t * req, int status )
{
    FUNCTION_CALL

    if ( status )
    {
        fprintf ( stdout, "remote connection failed, need to enable remote service\n" );

        if ( uv_last_error ( req->handle->loop ).code != UV_ECANCELED )
        {
            SHOW_UV_ERROR ( req->handle->loop );
            free ( req->data );
            free ( req );
        }
        return;
    }

    session_t * session = ( session_t * ) req->data;
    //fprintf ( stdout, "cmd_line[%d]:\n%s\n", session->cmd_line_.length( ), ( char * ) session->cmd_line_.c_str( ) );

    /**
     * uv_buf_t buf[1];
     * buf[0].len = len;
     * buf[0].base = message;
     */

    /* buf init */
#if 0
    // Here it works on both Windows-7 and Linux
    char buf_tmp[4096] = { 0 };
    uv_buf_t buf = uv_buf_init ( buf_tmp, session->cmd_line_.length( ) );
    buf.base = ( char * ) session->cmd_line_.c_str( );
#else
    // TODO
    // It works on Linux, but dosen't work on Windows-7
    // On Windows-7, here it will lead program quit without any warning or error.
    // It even will not call on_write_end() function after uv_write() called normally.
    uv_buf_t buf = uv_buf_init ( ( char * ) session->cmd_line_.c_str( ), session->cmd_line_.length( ) );
#endif

    uv_stream_t * tcp = req->handle;

    uv_write_t write_req;

    int buf_count = 1;

    int rc = uv_write ( & write_req, tcp, & buf, buf_count, on_write_end );
    if ( rc )
    {
        SHOW_UV_ERROR ( req->handle->loop );
        fprintf ( stderr, "uv_write() called failed\n" );
    }
    else
    {
        fprintf ( stderr, "uv_write() called normally.\n" );
    }
}


--
     _                         __ _     _
 ___| |__   ___ _ __   __ _   / _(_)___| |__
|_  / '_ \ / _ \ '_ \ / _` | | |_| / __| '_ \
 / /| | | |  __/ | | | (_| | |  _| \__ \ | | |
/___|_| |_|\___|_| |_|\__, | |_| |_|___/_| |_|
                      |___/

Fedor Indutny

unread,
Jan 22, 2014, 7:04:45 AM1/22/14
to li...@googlegroups.com
Hello!

I'd suspect that it is a compiler bug, seems like it thinks that
`c_str()` is no longer required after `uv_buf_init()` call. Could you
please try compiling with disabled optimizations to make sure that we
are right?

Cheers,
Fedor.
> --
> 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 libuv+un...@googlegroups.com.
> To post to this group, send email to li...@googlegroups.com.
> Visit this group at http://groups.google.com/group/libuv.
> For more options, visit https://groups.google.com/groups/opt_out.

Fish Zheng

unread,
Jan 23, 2014, 3:54:27 AM1/23/14
to li...@googlegroups.com, fe...@indutny.com
Hi, Fedor
    Thanks for your feedback.
    I don't use any optimizations parameters.

    However now I just change the variable 'session->cmd_line_' as one std::string ptr type, then it works on Windows-7.

Fish Zheng

unread,
Jan 23, 2014, 7:50:39 PM1/23/14
to li...@googlegroups.com, fe...@indutny.com
Hi,
I found that it was NOT caused by uv_buf_init with std::string, it was caused by write requirement nearby the uv_buf_init.

Thx.

#if 0
    ///It will crash on Windows7, but work on Linux
    ///uv_write_t write_req;
#else
    uv_write_t *write_req = new uv_write_t();
#endif
Reply all
Reply to author
Forward
0 new messages