测试std :: wstring长度的分段错误

时间:2012-07-05 07:38:31

标签: c++ std wstring

我只是不明白。我有一个从List中读取词干字符串的函数,检查每个字符串是否包含标点符号并在单个单词图像的循环中构建短语。当我用 s.length()>检查字符串构建/包含短语时if语句中的0 我得到一个seg错误。这是功能:

std::wstring
Phrase::userFriendlyTerms() {
    std::wstring s = L"";

    List<Object> *terms = getTerms();

    Iterator<Object> *i = terms->iterator();
    for (; i->hasNext();) {
        StemmedTerm *t = (StemmedTerm *) i->next();
        std::wstring image = t->getTerm();

        // --- gdb shows me a seg. fault with s.length()
        if (s.length() > 0 &&
            !((image.compare(L",") == 0) || (image.compare(L"?" == 0))
                || (image.compare(L"!") == 0)
                || (image.compare(L";") == 0))) {
            s.append(L" ");
        }
        s.append(image);
    }   
    delete i;

    return s;
}

确切的错误信息是:

Program received signal SIGSEGV, Segmentation fault.
__wcslen (s=0x0) at wcslen.c:30
30  wcslen.c: No such file or directory.
    in wcslen.c
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) n
Program not restarted.
(gdb) backtrace
#0  __wcslen (s=0x0) at wcslen.c:30
#1  0x00007ffff7b89e56 in std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::compare(wchar_t const*) const () from /usr/lib/libstdc++.so.6
#2  0x00000000004833ed in Phrase::userFriendlyTerms (this=0x1425ac0) at algorithm/Phrase.cpp:56

也许我失明或遗失某些东西......

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

这里看起来很可疑

(image.compare(L"?" == 0)

你确定你没有意思

(image.compare(L"?") == 0)