Hello!

You don't need to `uv_close()` any request, just release the memory in
their callbacks.
This applies to `uv_write_t`, `uv_connect_t` and everything that doesn't
inherit from handle.

`uv_close()` is closing underlying socket, so please call it only when you
are done with the socket and
do not want to receive or send anything on it.

Cheers,
Fedor.


On Wed, Jul 2, 2014 at 1:40 PM, Claudio Bisegni <[email protected]> wrote:

> Hi i have some problem to well understand the libuv functionality on when
> deallocate the handler and stream.
>
> i have a cpp class that have a one uv_tcp_t instance for the client. When
> i need to send data to one server i do the following step:
>
> i use a struct RequestInfo * ri to keep the reference to the
> tcp_connection for close it.
>
> 1) allocate the connection with ri->tcp_connection = (uv_connect_t*)malloc
> (sizeof(uv_connect_t));
> 2) call the uv_tcp_connect(ri->tcp_connection, &client, (const struct
> sockaddr*)&req_addr, TCPUVClient::on_connect); where the 'client' is the
> unique instance of uv_tcp_t;
> 3) below my on connect:
>
> void TCPUVClient::on_connect(uv_connect_t *connection, int status) {
>
> RequestInfo *ri = static_cast<RequestInfo*>(connection->data);
>
> if (status) {
>
> TCPUVClientLERR << "error on_connect" << status;
>
> uv_close((uv_handle_t*)connection,TCPUVClient::on_close);
>
> DEALLOCATE_REQUEST_INFO(ri)
>
> return;
>
> }
>
>
>  //get the data to send
>
> ri->serializationBuffer = ri->messageInfo->message->getBSONData();
>
>  //initialize uv buffer
>
> uv_buf_t buf = uv_buf_init((char*)ri->serializationBuffer->getBufferPtr(),
> (unsigned int)ri->serializationBuffer->getBufferLen());
>
>  //connect data to stream handler
>
> connection->handle->data = connection->data;
>
>  uv_write_t request;
>
> uv_write(&request, connection->handle, &buf, 1, TCPUVClient::on_write_end);
>
> }
>
>
> 4) after the data is sent i need to read the ack answer with:
>
> void TCPUVClient::on_ack_read(uv_stream_t *stream, ssize_t nread, const
> uv_buf_t* buf) {
>
> //delete the forward info class
>
> RequestInfo *ri = static_cast<RequestInfo*>(stream->data);
>
> stream->data = NULL;
>
>  if (nread>0) {
>
> auto_ptr<chaos::common::data::CDataWrapper> ack_result(new
> chaos::common::data::CDataWrapper(buf->base));
>
> LDBG_ << "TCPUVClient::on_ack_read message
> result------------------------------";
>
> LDBG_ << ack_result->getJSONString();
>
> LDBG_ << "TCPUVClient::on_ack_read message
> result------------------------------";
>
> //uv_close((uv_handle_t*) server, TCPUVClient::on_close);
>
> return;
>
> }
>
> uv_close((uv_handle_t*)stream, TCPUVClient::on_close);
>
> free(buf->base);
>
> DEALLOCATE_REQUEST_INFO(ri)
>
> }
>
>
> void TCPUVClient::on_write_end(uv_write_t *req, int status) {
>
> RequestInfo *ri = static_cast<RequestInfo*>(req->data);
>
> if (status) {
>
> TCPUVClientLERR << "error on_write_end ->" << status;
>
> uv_close((uv_handle_t*)ri->tcp_connection,TCPUVClient::on_close);
>
> DEALLOCATE_REQUEST_INFO(ri)
>
> return;
>
> }
>
>  uv_read_start(req->handle, TCPUVClient::alloc_buffer,
> TCPUVClient::on_ack_read);
>
> }
>
>
> my question is when i need to call uv_close for:
>
> *uv_write_t *req* contained in on_write_end
>
> *uv_stream_t *stream* contained in on_ack_read
>
> *uv_connect_t** *connection* contained in on_connection
>
>
> the client work but i don't now if i forgot to free memory.
>
>
> thanks in advanced for all group. And there is a good example on how work
> well with libuv?
>
> --
> 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