2011-09-22 18 views
6

Valgrindがsubstr呼び出しで不平を言っています。このValgrindエラーの原因は何ですか?

string Message::nextField(string& input) { 
    int posSeparator = input.find_first_of(SEPARATOR); 
    string temp; 
    temp = input.substr(0, posSeparator); //Error points to this line 
    input.erase(0, posSeparator + 1); 
    return temp; 
} 

エラーが行く:12個のブロックで
290バイトは間違いなく損失レコード1機能は、基本的には区切り文字で区切られた文字列の部分を返し、入力を解析している何
1の中で失われています。この機能は、次の定義を持つ別のクラスのメソッドから呼び出されます。

void doSomething(string input) { 
    input.erase(0,2); 
    string temp = nextField(input); 
    this->room = atoi(temp.c_str()); 
    temp = input; 
    this->money = atoi(temp.c_str()); 
} 

十分にここに含まれる奇妙なまたは重要な他に何もありません。 Eclipse IndigoのValgrindプロファイリングのValgrindのデフォルト設定を使用します。 アイデア

+3

あなたは、コンパイル時に最適化されていますか?もしそうなら、しないでください。これはvalgrindからの偽の報告をたくさん引き起こします。 –

+0

'string temp = input.substr(0、posSeparator);'と書くと、文字列を代入する代わりに初期化することができます。しかし、それがあなたの問題に関係することは明らかではありません。 –

+0

@DavidHammenいいえ、私はそうではありません。 – Erandros

答えて

0

posSeparatorが実際にstring :: nposと異なるかどうかはチェックしません。これにより、消去に問題が発生する可能性があります。それは野生のショットですが、とにかくバグを修正するかもしれません。

1

あなたはおそらく、あなたの元にどこかにエラーが発生しています。だから、

HEAP SUMMARY: 
    in use at exit: 0 bytes in 0 blocks 
    total heap usage: 2 allocs, 2 frees, 61 bytes allocated 

All heap blocks were freed -- no leaks are possible 

For counts of detected and suppressed errors, rerun with: -v 
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4) 

、おそらくあなたの問題はではありません。そして、

#include <string> 
#include <iostream> 
#include <cstdlib> 

using namespace std; 

const char SEPARATOR = ':'; 

struct Foo 
{ 
public: 
    int room; 
    int money; 

    void doSomething(string input) { 
     input.erase(0,2); 
     string temp = nextField(input); 
     this->room = atoi(temp.c_str()); 
     temp = input; 
     this->money = atoi(temp.c_str()); 
    } 

    string nextField(string& input) { 
     int posSeparator = input.find_first_of(SEPARATOR); 
     string temp; 
     temp = input.substr(0, posSeparator); //Error points to this line 
     input.erase(0, posSeparator + 1); 
     return temp; 
    } 
}; 

int main() 
{ 
    Foo f; 
    f.doSomething("--234:12"); 
    std::cout << f.room << " - " << f.money << std::endl; 
} 

valgrindのRAN:

valgrind --tool=memcheck <executable> 

と出力されたが、私は次のコードを使用してエラーを再現してみましたコード

2

のこの部分は、それはおそらくあなたのコードのバグではありません。このエラーは、C++標準ライブラリの実装の詳細により報告される可能性があります。これはValgrind FAQから次のことを試してください確認するには:GCC 2.91、2.95、3.0で

と3.1、-D__USE_MALLOCでSTL を使用して、すべてのソースをコンパイルします。注意してください!これは、 バージョン3.3から始まるGCCから削除されました。

GCC 3.2.2以降では、プログラムを実行する前に環境変数 GLIBCPP_FORCE_NEWをエクスポートする必要があります。 GCC 3.4以降で

、その変数は GLIBCXX_FORCE_NEWに名前を変更しました。

関連する問題