调试时出现堆栈溢出,但没有释放
|
我下面有以下代码,该代码分析文本文件并索引单词和行:
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;
}
没有找到相关结果
已邀请:
4 个回复
先对冈蒲
这应该阻止您在每次嵌套的addWord调用上复制word对象。 还可以考虑添加std :: cout << \“递归addWord \” << std :: endl;在addWord上键入语句,以便您可以查看其执行的深度以及是否正确终止。
呢率篓舍烫
肉簧咸缮
蹄渭信妥扳