I runing this code and I getting this message from Valgrind, I new in c++ and Linux but I know that I have to fix it. please can you advise what is wrong?. I am using Ubuntu.
The message is this:
==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)
the code is: int down[2], up[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.
the function readre_wrapper is:
THREAD_Return GmpPipePlayer::reader_wrapper(void *p)
{
GmpPipePlayer *t = (GmpPipePlayer *)p;
t->reader_fn();
return NULL;
}
this new Thread is called many times. before I call it again i do the following:
if (_pid > 0)
{
kill(_pid, SIGTERM);
_pid = 0;
}
if (_up)
{
close(_up);
_up = 0;
}
if (_down)
{
close(_down);
_down = 0;
}
Any ideas what is wrong?
Well, you have a new
that's never matched by a delete
. You'll need a delete _reader_thd;
at some point in your code. Or not use dynamic objects at all.