Hello, everyone.

I've written the following simple test program that copies its stdin to stdout:

------- 8< ------- 8< ------- 8< --------
#include <uv.h>
#include <stdlib.h>
#include <stdio.h>

#define PRINT_UV_ERR(prefix, code)  do {\
    fflush(stdout);\
    fprintf(stderr, "%s: %s (%i): %s\n", prefix, uv_err_name(code), 
(int)(code), uv_strerror(code));\
    fflush(stderr);\
} while (0)


/* assuming  that stdin and stdout can be handled with as pipes */
uv_pipe_t in, out;


void alloc_cb(uv_handle_t*, size_t, uv_buf_t*);
void read_cb(uv_stream_t*, ssize_t, const uv_buf_t*);
void write_cb(uv_write_t*, int);


int main(int _argc, char *_argv[])
{
  int ret = 0;

  uv_loop_t *loop = uv_default_loop();

  uv_pipe_init(loop, &in, 0);
  ret = uv_pipe_open(&in, fileno(stdin));
  if (ret < 0)  {
    PRINT_UV_ERR("stdin open", ret);
    return ret;
  }

  uv_pipe_init(loop, &out, 0);
  ret = uv_pipe_open(&out, fileno(stdout));
  if (ret < 0)  {
    PRINT_UV_ERR("stdout open", ret);
    return ret;
  }

  uv_read_start((uv_stream_t*)&in, alloc_cb, read_cb);

  return uv_run(loop, UV_RUN_DEFAULT);
}



void alloc_cb(uv_handle_t *_handle, size_t _suggested_size, uv_buf_t *_buf)
{
  /* allocate the memory for a new I/O buffer */
  *_buf = uv_buf_init((char*)malloc(_suggested_size), _suggested_size);
}


void read_cb(uv_stream_t *_stream, ssize_t _nread, const uv_buf_t *_buf)
{
  if (_nread < 0)
  {
    uv_read_stop(_stream);
    if (_nread != UV_EOF)  PRINT_UV_ERR("read", _nread);
  }
  else if (_nread > 0)
  {
    /* initialize a new buffer descriptor specifying the actual data length */
    uv_buf_t buf = uv_buf_init(_buf->base, _nread);

    /* create a write request descriptor */
    uv_write_t *wr = (uv_write_t*)malloc(sizeof(uv_write_t));

    /* save a reference to the output buffer somehow along with the write 
request */
    wr->data = _buf->base;

    /* fire up the write request */
    uv_write(wr, (uv_stream_t*)&out, &buf, 1, write_cb);

    /* the I/O buffer being used up should be deleted somewhere */
  }
}


void write_cb(uv_write_t *_wr, int _status)
{
  if (_status < 0)
  {
    PRINT_UV_ERR("write", _status);
    uv_read_stop((uv_stream_t*)&in);
  };

  /* when the write request has completed it's safe to free up the memory 
allocated for the I/O buffer */
  free(_wr->data);

  /* delete the write request descriptor */
  free(_wr);
}
------- >8 ------- >8 ------- >8 --------



And when I am running it with the following command line on Linux:
$ cat /dev/zero | ./cpio-uv | dd of=/dev/null iflag=fullblock bs=1M count=100

I observe that the memory used by the program is constantly growing during the 
program run until the program ends
and it is seen like if free() functions in write_cb() is not ever called.
And if the amount of data being pumping would be increased in dd count 
parameter up to the 500
then the available physical memory of 8G becomes exhausted and the swapping 
process begins slowing down the system response.

The build command is:
$ c++ -std=c++1y -Wall -Wpedantic -O2  ./example/cpio-uv.c  -luv -o cpio-uv

or

$ gcc -std=c99  -Wall -Wpedantic -O2 -D _DEFAULT_SOURCE  ./example/cpio-uv.c  
-luv -o cpio-uv

$ gcc --version
gcc (GCC) 5.3.1 20160406 (Red Hat 5.3.1-6)


Have anyone come across with this issue? 


The problem does not appear on Windows where I use Mingw-64 with Cygwin shell.

$ x86_64-w64-mingw32-c++ -std=c++1y -Wall -Wpedantic -O2 \
                         -I 
/cygdrive/d/wroot/libuv/uvcc/uvcc.git/libuv-x64-v1.9.0.build9/include \
                         -D _WIN32_WINNT=0x0601 -static-libgcc 
-static-libstdc++    ./example/cpio-uv.c \
                         -L 
/cygdrive/d/wroot/libuv/uvcc/uvcc.git/libuv-x64-v1.9.0.build9 -luv -lWs2_32 \
                         -o cpio-uv

$ x86_64-w64-mingw32-c++ --version
x86_64-w64-mingw32-c++ (GCC) 4.9.2


Thanks.

-- 
Mike

-- 
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