libuv + C ++段错误

时间:2015-03-28 16:11:58

标签: c++ libuv

我正在用libuv编写C ++流的包装器,但是它是段错误的,我找不到原因。流类:

class Stream{
  public:
  Stream(int fd);

  void Write(const std::string& data, const std::function<void(void)>& callback);


  const static int STDIN = 0;
  const static int STDOUT = 1;
  private:
  static void OnWrite(uv_write_t *req, int status);
  uv_pipe_t pipe;
};

实施

Stream::Stream(int fd){
  uv_pipe_init(uv_default_loop(), &pipe, 0);
  uv_pipe_open(&pipe, fd);
}

void Stream::Write(const std::string& data, const std::function<void(void)>& callback){
  uv_write_t request;
  uv_buf_t buffer = uv_buf_init((char*)malloc(data.length()*sizeof(char)), data.length());
  strcpy(buffer.base, data.c_str());
  buffer.len = data.length();
  uv_write(&request, (uv_stream_t*)&pipe, &buffer, 1, OnWrite);
}

void Stream::OnWrite(uv_write_t *req, int status){

}

在主文件中我这样做:

Stream* out = new Stream(Stream::STDOUT);
  out->Write("Hello, World!", []() {
});

uv_run(uv_default_loop(), RUN_DEFAULT);

然而,一旦我运行它,就会在uv_run语句上发生段错误。回溯:

#0  0x00007ffff7bcac94 in uv__write_callbacks (stream=stream@entry=0x603010) at src/unix/stream.c:907
#1  0x00007ffff7bcb0a1 in uv__stream_io (loop=<optimized out>, w=0x603098, events=<optimized out>) at src/unix/stream.c:1243
#2  0x00007ffff7bc31eb in uv__run_pending (loop=0x7ffff7dd98c0 <default_loop_struct>) at src/unix/core.c:710
#3  uv_run (loop=0x7ffff7dd98c0 <default_loop_struct>, mode=UV_RUN_DEFAULT) at src/unix/core.c:316
#4  0x0000000000400fcd in main ()

但是,如果我将uv_run放在Stream::Write方法中,它就可以了。我该如何解决这个问题?


修改

我现在在堆上而不是在堆栈上分配管道,但它仍然给我一个段错误。我将标题中的uv_pipe_t更改为uv_pipe_t*,现在实现已更改为:

Stream::Stream(int fd){
  pipe = new uv_pipe_t;
  uv_pipe_init(uv_default_loop(), pipe, 0);
  uv_pipe_open(pipe, fd);
}

void Stream::Write(const std::string& data, const std::function<void(void)>& callback){
  uv_write_t request;
  uv_buf_t buffer = uv_buf_init((char*)data.c_str(), data.length());
  uv_write(&request, (uv_stream_t*)pipe, &buffer, 1, OnWrite);
}

void Stream::OnWrite(uv_write_t *req, int status){
  delete req;
}

GDB的Backtrace:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bcac94 in uv__write_callbacks (stream=stream@entry=0x603030) at src/unix/stream.c:907
907         uv__req_unregister(stream->loop, req);
(gdb) backtrace
#0  0x00007ffff7bcac94 in uv__write_callbacks (stream=stream@entry=0x603030) at src/unix/stream.c:907
#1  0x00007ffff7bcb0a1 in uv__stream_io (loop=<optimized out>, w=0x6030b8, events=<optimized out>) at src/unix/stream.c:1243
#2  0x00007ffff7bc31eb in uv__run_pending (loop=0x7ffff7dd98c0 <default_loop_struct>) at src/unix/core.c:710
#3  uv_run (loop=0x7ffff7dd98c0 <default_loop_struct>, mode=UV_RUN_DEFAULT) at src/unix/core.c:316
#4  0x0000000000400f4d in main ()

1 个答案:

答案 0 :(得分:2)

您的“写入请求”(uv_write_t)在堆栈上分配,并且在libuv尝试将其指针传递给写回调函数时不存在(或者,实际上当libuv本身尝试使用它时。)请记住,libuv中的一切都是异步的;这可能就是你使用它的原因!

你应该在堆上分配你的uv_write_t(或者作为全局/类变量或从池或其他任何东西),并记住在写回调中释放它。

程序中还有另一个内存/生命周期错误,但这个错误不是崩溃错误,而是垃圾生成错误。您malloc放入uv_buffer_t的缓冲区不需要,可以在调用后立即释放(这个是同步的!)实际上,您甚至不需要分配单独的缓冲区;您可以将data.c_str()作为第一个参数传递给uv_buf_init()