简体   繁体   English

C ++地图 <string,string> :: find似乎返回垃圾迭代器

[英]c++ map<string,string>::find seems to return garbage iterator

map<string,string>::find seems to be returning garbage iterator, since i can access neither my_it->first nor second (NB: my_it != my_map.end() is verified). map<string,string>::find似乎正在返回垃圾迭代器,因为我无法访问my_it->first或second(注意: my_it != my_map.end()已验证)。 VC2010 reports a debug error, and looking deeper reveals VC2010报告调试错误,并且深入了解

my_it is (Bad Ptr, Bad Ptr).

The 'offending' map is a class attribute, _match , shown below in context: “违规”映射是一个类属性_match ,在上下文中显示如下:

class NicePCREMatch
{
private:
    map<string, string, less<string> > _match;

public:
    void addGroup(const string& group_name, const string& value);
    string group(const string& group_name);
};

Here is the code that returns elements by key (the commented-out code works fine): 这是按键返回元素的代码(注释掉的代码可以正常工作):

string NicePCREMatch::group(const string& group_name)
{
    /*for (map<string, string, less<string> >::iterator j = _match.begin(); j != _match.end(); j++)
    {
        if(!strcmp(j->first.c_str(), group_name.c_str()))
        {
            return j->second;
        }
    }

    throw runtime_error("runtime_error: no such group");*/

    map<string, string, less<string> >::iterator i = _match.find(group_name);

    if (i == _match.end())
    {
        throw runtime_error("runtime_error: no such group");
    }

    return i->second;
}

And Here is the code that inserts new elements in the map: 这是在地图中插入新元素的代码:

void NicePCREMatch::addGroup(const string& group_name, const string& value)
{
    _match.insert(pair<string, string>(group_name, value));
}

Another class uses NicePCREMatch as follows: 另一类使用NicePCREMatch如下:

template<class Match_t>
vector<Match_t> NicePCRE<Match_t>::match(const string& buf)
{
[snip]
    Match_t m;
[snip]
    m.addGroup(std::string((const char *)tabptr + 2, name_entry_size - 3), \
                buf.substr(ovector[2*n], ovector[2*n+1] - ovector[2*n]));
[snip]
    addMatch(m);
[snip]
    return _matches;
}

Where, 哪里,

template<class Match_t>
void NicePCRE<Match_t>::addMatch(const Match_t& m) 
{ 
    _matches.push_back(m);
}

Finally, client code uses NicePCRE class as follows: 最后,客户端代码使用NicePCRE类,如下所示:

void test_NicePCRE_email_match(void)
{
    NicePCRE<> npcre;
    npcre.compile("(?P<username>[a-zA-Z]+?)(?:%40|@)(?P<domain>[a-zA-Z]+\.[a-zA-Z]{2,6})");
    vector<NicePCREMatch> matches = npcre.match("toto@yahoo.com");
    assert(!matches.empty());
    assert(!strcmp(matches.begin()->group("username").c_str(), "toto"));
    cout << matches.begin()->group("domain").c_str() << endl;
    assert(!strcmp(matches.begin()->group("domain").c_str(), "yahoo.com"));
}

BTW, this --is pretty much-- my main (the oddest TDD ever :) ): 顺便说一句,这几乎是我的主要(有史以来最奇怪的TDD :)):

int main()
{
    int test_cnt = 0;
    cout << "Running test #" << test_cnt << " .." << endl;
    test_NicePCRE_email_match();
    cout << "OK." << endl << endl;
    test_cnt++;

    SleepEx(5000, 1);

    return 0;
}

What am I doing wrong here? 我在这里做错了什么?

EDIT: The following modification (compare with the version above) solved my problem. 编辑:以下修改(与上述版本比较)解决了我的问题。 Viz, Viz,

void NicePCREMatch::addGroup(const string& group_name, const string& value)
{
    _match.insert(pair<string, string>(group_name.c_str(), value.c_str()));
}

Client code (slightly modified) now looks like this: 客户端代码(稍作修改)现在如下所示:

void test_NicePCRE_email_match(void)
{
    NicePCRE<> npcre;
    npcre.compile("(?P<username>[a-zA-Z]+?)(?:%40|@)(?P<domain>[a-zA-Z]+\.[a-zA-Z]{2,6})");
    vector<NicePCREMatch> matches = npcre.match("toto@yahoo.com");
    assert(!matches.empty());
    try
    {
        assert(!strcmp(matches.begin()->group("username").c_str(), "toto"));
        assert(!strcmp(matches.begin()->group("domain").c_str(), "yahoo.com"));
        cout << "username = " << matches.begin()->group("username") << endl;
        cout << "domain = " << matches.begin()->group("domain") << endl;
    }
    catch (const runtime_error& e)
    {
        cout << "Caught: " << e.what() << endl;
        assert(0x0);
    }
}

This is quite bizarre. 这很奇怪。 Can someone please explain. 有人可以解释一下。 However, I consider my problem solved already. 但是,我认为我的问题已经解决。

Thanks every one. 感谢大家。

This might be caused by three things - either you modify the map in some way after the execution of find or you have a memory coruption somewhere in your program or the debugger is simply not showing the correct values for the iterator. 这可能是由三件事引起的-在执行find之后以某种方式修改映射,或者在程序中某处发生内存损坏,或者调试器根本没有为迭代器显示正确的值。

Try using debug output - if the code crashes when you try to output the values, then probably the iterator is really broken. 尝试使用调试输出-如果在尝试输出值时代码崩溃,则可能是迭代器确实损坏了。

Also make sure you do not modify the map after the execution of find. 另外,请确保在执行find之后不要修改地图。 If you do, this may make the iterator invalid and so you need to move the find call immedietly before using the iterator. 如果这样做,可能会使迭代器无效,因此您需要在使用迭代器之前立即移动find调用。

If both of the above options don't help you probably have memory corruption somewhere and you need to find it. 如果以上两个选项都不能帮助您,则可能是内存损坏,您需要找到它。 Maybe use valgrind for that. 也许为此使用valgrind Please note this should be your last resort only when the two other options are proved impossible. 请注意,只有在其他两个选项均无法实现的情况下,这才是您的最后选择。

Your issue is here 您的问题在这里

if (i == _match.end())
{
    throw runtime_error("runtime_error: no such group");
}

return i->second;

Your find failed for some reason. 您的搜索由于某种原因而失败。 I can't say why because I don't have the full code. 我不能说为什么,因为我没有完整的代码。 But, after the failure, you are throwing an error, but there is nobody to catch outside. 但是,在失败之后,您将抛出一个错误,但是没有人可以抓住。 Please add a try catch at the point where you call the method group() and implement the logic if the match is not found. 请在调用方法group()的位置添加一个try catch,并在找不到匹配项的情况下实现逻辑。 I tried with your sample snippets (+ some changes to get the stuff compiled) and it looks like visual studio continues with the next line in the function even after a throw statement. 我尝试使用您的示例代码片段(进行一些更改以编译内容),即使在throw语句之后,Visual Studio仍会继续执行函数的下一行。 I don't know the theory behind it. 我不知道背后的理论。 I was bit surprised at seeing such a behavior. 看到这样的行为我感到很惊讶。

[To make sure that your class structure is not causing the problem, I tried with a simple global method and even the method also gave me the same behavior. [为了确保您的类结构不会引起问题,我尝试了一个简单的全局方法,甚至该方法也给了我相同的行为。 If there are somebody who can explain this please feel free.] 如果有人可以解释这一点,请放心。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM