将文档添加到Lucene索引导致崩溃
题
我试图将仅使用一个ID3帧的MP3文件索引。使用Clucene和Taglib。以下代码正常:
...
TagLib::MPEG::File file("/home/user/Depeche Mode - Personal Jesus.mp3");
if (file.ID3v2Tag()) {
TagLib::ID3v2::FrameList frameList = file.ID3v2Tag()->frameList();
lucene::document::Document *document = new lucene::document::Document;
TagLib::ID3v2::FrameList::ConstIterator frame = frameList.begin();
std::wstring field_name((*frame)->frameID().begin(), (*frame)->frameID().end());
const wchar_t *fieldName = field_name.c_str();
const wchar_t *fieldValue = (*frame)->toString().toWString().c_str();
lucene::document::Field field(fieldName, fieldValue, true, true, true, false);
document->add(field);
writer->addDocument(document);
}
...
但这使应用程序崩溃:
...
TagLib::MPEG::File file("/home/user/Depeche Mode - Personal Jesus.mp3");
if (file.ID3v2Tag()) {
TagLib::ID3v2::FrameList frameList = file.ID3v2Tag()->frameList();
lucene::document::Document *document = new lucene::document::Document;
for (TagLib::ID3v2::FrameList::ConstIterator frame = frameList.begin(); frame != frameList.end(); frame++) {
std::wstring field_name((*frame)->frameID().begin(), (*frame)->frameID().end());
const wchar_t *fieldName = field_name.c_str();
const wchar_t *fieldValue = (*frame)->toString().toWString().c_str();
lucene::document::Field field(fieldName, fieldValue, true, true, true, false);
document->add(field);
}
writer->addDocument(document);
}
...
这是为什么?!
解决方案
这是一个范围问题 - 当您致电Writer-> addDocument时,您添加的字段已释放。改用此代码:
document->add(* new lucene::document::Field(fieldName, fieldValue, true, true, true, false));
您可能需要查看cl_demo和cl_test以查看一些代码示例。
其他提示
您不需要构建一个新的lucene :: document ::每个标签字段吗?似乎您正在重复使用相同的地址,这是有问题的。我想调试器可以告诉您更多。
不隶属于 StackOverflow