2012-09-30 79 views
2

我運行此代碼,並從Valgrind收到此消息,我在c + +和Linux新,但我知道我必須解決它。請你能提醒有什麼不對?我正在使用Ubuntu。valgrind:塊肯定是丟失在損失記錄...在新線程

該消息是這樣的:

==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; 
} 

任何想法是什麼錯誤?

回答

3

那麼,你有一個new,這從來沒有匹配delete。您需要在代碼中的某個位置使用delete _reader_thd;。或根本不使用動態對象。

+1

謝謝!它現在不會發生。 – user1681210