调试时出现堆栈溢出,但没有释放

| 我下面有以下代码,该代码分析文本文件并索引单词和行:
bool Database::addFromFileToListAndIndex(string path, BSTIndex* & index, list<Line *> & myList)
{
    bool result = false;
    ifstream txtFile;
    txtFile.open(path, ifstream::in);
    char line[200];
    Line * ln;
    //if path is valid AND is not already in the list then add it
    if(txtFile.is_open() && (find(textFilePaths.begin(), textFilePaths.end(), path) == textFilePaths.end())) //the path is valid
    {
        //Add the path to the list of file paths
        textFilePaths.push_back(path);
        int lineNumber = 1;
        while(!txtFile.eof())
        {
            txtFile.getline(line, 200);
            ln = new Line(line, path, lineNumber);
            if(ln->getLine() != \"\")
            {
                lineNumber++;
                myList.push_back(ln);
                vector<string> words = lineParser(ln);
                for(unsigned int i = 0; i < words.size(); i++)
                {
                    index->addWord(words[i], ln);
                }
            }
        }
        result = true;
    }
    return result;
}
我的代码可以完美,快速地工作,直到我给它一个巨大的文本文件。然后我从Visual Studio中收到堆栈溢出错误。当我切换到“发布”配置时,代码运行顺利。我的代码有问题吗?运行“ Debug”配置时是否存在某种限制?我是否想在一项功能中做太多事情?如果是这样,我如何分解它,使其在调试时不会崩溃? 编辑 根据请求,我执行addWord;
void BSTIndex::addWord(BSTIndexNode *& pCurrentRoot, string word, Line * pLine)
    {
        if(pCurrentRoot == NULL)  //BST is empty
        {
            BSTIndexNode * nodeToAdd = new BSTIndexNode();
            nodeToAdd->word = word;
            nodeToAdd->pData = pLine;
            pCurrentRoot = nodeToAdd;
            return;
        }
        //BST not empty
        if (word < (pCurrentRoot->word)) //Go left
        {
            addWord(pCurrentRoot->pLeft, word, pLine);
        }
        else //Go right
        {
            addWord(pCurrentRoot->pRight, word, pLine);
        }
    }
和lineParser:
vector<string> Database::lineParser(Line * ln) //Parses a line and returns a vector of the words it contains
{
    vector<string> result;
    string word;
    string line = ln->getLine();
    //Regular Expression, matches anything that is not a letter, number, whitespace, or apostrophe
    tr1::regex regEx(\"[^A-Za-z0-9\\\\s\\\\\']\");
    //Using regEx above, replaces all non matching characters with nothing, essentially removing them.
    line = tr1::regex_replace(line, regEx, std::string(\"\"));

    istringstream iss(line);
    while(iss >> word)
    {
        word = getLowercaseWord(word);
        result.push_back(word);
    }
    return result;
}
    
已邀请:
堆栈溢出表明您已经用完了堆栈空间(可能很明显,但以防万一)。典型的原因是不终止或过度递归,或者堆栈对象重复很大。有趣的是,在这种情况下可能是这样。 在Release中,您的编译器可能正在进行尾部调用优化,以防止过多的递归导致堆栈溢出。 还可能是在Release中,编译器正在优化lineParser中向量的返回副本。 因此,您需要找出在Debug中溢出的条件,我将以递归作为最可能的罪魁祸首,尝试将字符串参数类型更改为引用,即。
void BSTIndex::addWord(BSTIndexNode *& pCurrentRoot, string & word, Line * pLine)
这应该阻止您在每次嵌套的addWord调用上复制word对象。 还可以考虑添加std :: cout << \“递归addWord \” << std :: endl;在addWord上键入语句,以便您可以查看其执行的深度以及是否正确终止。     
问题几乎可以肯定是addWord中的递归调用-在未优化的构建中,这将消耗大量的堆栈空间,而在优化的构建中,编译器会将其转换为尾部调用,从而重新使用同一堆栈框架。 您可以轻松地将递归调用手动转换为循环:
void BSTIndex::addWord(BSTIndexNode ** pCurrentRoot, string word, Line * pLine)
{
    while (*pCurrentRoot != NULL) {
        //BST not empty
        if (word < (*pCurrentRoot)->word) //Go left
        {
            pCurrentRoot = &(*pCurrentRoot)->pLeft;
        }
        else //Go right
        {
            pCurrentRoot = &(*pCurrentRoot)->pRight;

        }
    }
    //BST is empty
    BSTIndexNode * nodeToAdd = new BSTIndexNode();
    nodeToAdd->word = word;
    nodeToAdd->pData = pLine;
    *pCurrentRoot = nodeToAdd;
}
    
您还应该发布堆栈,这实际上将显示导致溢出的原因。很明显,addWord中的递归会大量消耗堆栈内存。 如果只想让它工作,请进入编译器/链接器设置,并增加为堆栈保留的大小。默认情况下,它只有1MB,最多可以增加到32MB,并且您可以放心调试构建所具有的任何额外的计数器或探针,您将有足够的堆栈来处理它。     
您可以将堆栈的大小增加到适当的字节数。
#pragma comment(linker, \"/STACK:1000000000\")  
    

要回复问题请先登录注册