C++ string tokenizer is causing heap corruption on push_back

  c++

I have the following method:

vector<string> tokenize(const string& s, const string& separator)
{
    vector<string> tokens;
    string currentToken;
    
    string::const_iterator i;
    for(i = s.begin(); i != s.end(); ++i)
    {
        char c = (*i);

        if(matchesIn(c, separator)) 
        {
            tokens.push_back(currentToken);
            currentToken.clear();
        }
        else
        {
            currentToken += c;
        }
    }
    if(currentToken.size() > 0)
        tokens.push_back(currentToken);

    return tokens;
}

The goal here is to split up a string by the given separator. However when running this the second last line tokens.push_back(currentToken); is causing a heap allocation error:

Unhandled exception at 0x77526C43 (ntdll.dll) in Subspace3d.exe: 0xC0000374: A heap has been corrupted (parameters: 0x77563960).

Any ideas what could be wrong here?

Source: Windows Questions C++

LEAVE A COMMENT