【问题标题】:valgrind: blocks are definitely lost in loss record... in new threadvalgrind:块肯定会丢失在损失记录中......在新线程中
【发布时间】:2012-10-01 06:55:17
【问题描述】:

我运行此代码并从 Valgrind 收到此消息,我是 c++ 和 Linux 的新手,但我知道我必须修复它。请你能告诉我有什么问题吗?我正在使用 Ubuntu。

消息是这样的:

==29304== Thread 1:
==29304== 72 bytes in 18 blocks are definitely lost in loss record 55 of 89
==29304==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==29304==    by 0x4032BBC: GmpPipePlayer::GmpPipePlayer(IOBase*, Referee*, unsigned char, int, DataBoard const*, int, char const*, int) (unixgmppipe.cpp:126)
==29304==    by 0x40329F9: GmpPipePlayer::CreateFunc(IOBase*, Referee*, unsigned char, int, DataBoard const*, void*) (unixgmppipe.cpp:55)

代码是: int 向下[2],向上[2];

pipe(down);   
pipe(up);

_pid = fork();

if (_pid < 0)
    exit(1);


if (_pid == 0)
{
    close(down[1]);
    close(up[0]);

    dup2(down[0], 0);
    dup2(up[1], 1);

    execl("/bin/sh", "sh", "-c", cmd_line, NULL);

    _exit(1);
}

close(down[0]);
close(up[1]);
_down = down[1];
_up = up[0];

_reader_thd = new Thread(reader_wrapper, this); //here is the error happening.

函数 readre_wrapper 是:

THREAD_Return GmpPipePlayer::reader_wrapper(void *p)

{
    GmpPipePlayer *t = (GmpPipePlayer *)p;

    t->reader_fn();

    return NULL;
}

这个新线程被多次调用。在我再次调用它之前,我会执行以下操作:

if (_pid > 0)
{
    kill(_pid, SIGTERM);
    _pid = 0;
}

if (_up)
{
    close(_up);
    _up = 0;
}

if (_down)
{
    close(_down);
    _down = 0;
}   

有什么想法吗?

【问题讨论】:

    标签: c++ boost-thread


    【解决方案1】:

    好吧,你有一个 new 永远不会与 delete 匹配。在代码中的某个时刻,您需要 delete _reader_thd;。或者根本不使用动态对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 2021-11-02
      • 1970-01-01
      • 2021-04-21
      • 2016-06-30
      • 2013-12-31
      • 1970-01-01
      相关资源
      最近更新 更多