0

Valgrind によって報告されたエラーがあります。

==5644== Conditional jump or move depends on uninitialised value(s)

これは type の変数で発生していますpid_t

私のコードは次のとおりです。

GmpPipePlayer::GmpPipePlayer( IOBase *pIO, Referee *pBack, PieceColor pc, int size, const DataBoard *pBd, int handicap, const char *cmd_line, int bDebug )
    : GmpPlayer(pIO, pBack, pc, size, pBd, handicap, bDebug)
{
    int down[2], up[2];
        pid_t _pid;  //here the var is declared

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

GmpPipePlayer::~GmpPipePlayer()
{
    if (_pid > 0)   //valgrind is reporting that the error is here!!
    {
        kill(_pid, SIGTERM);
        _pid = 0;
    }

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

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

それで、問題は初期化されていないことだと思い_pidます。この変数をどのように初期化する必要がありますか? 私はこのようにしてみました:

 pid_t _pid=0;

しかし、これはまだ同じエラーを引き起こしています。そのコードは、プロセス中に何度も呼び出されます。

4

1 に答える 1

2

2 つの変数が呼び出され_pidたようです - コンストラクターで宣言したローカル:

pid_t _pid;  //here the var is declared

そして、デストラクタでアクセスするもの:

if (_pid > 0)   //valgrind is reporting that the error is here!!

これらの変数は同じではありません。デストラクタでアクセスする変数は、グローバル変数またはインスタンス変数でなければなりません (可能性が高い)。

_pidコンストラクタからデストラクタに状態を渡すことに依存しているため、ローカル宣言をコンストラクタから削除し、必要に応じて他の宣言を初期化する必要があり_pidます。インスタンス変数の場合は、次のように初期化リストに初期化を追加します。

GmpPipePlayer::GmpPipePlayer( IOBase *pIO, Referee *pBack, PieceColor pc, int size, const DataBoard *pBd, int handicap, const char *cmd_line, int bDebug )
: GmpPlayer(pIO, pBack, pc, size, pBd, handicap, bDebug), _pid(0) {
    ... //                         HERE ------------------^
}
于 2012-10-03T01:03:02.507 に答える