将文档添加到 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);
}
...
这是为什么?!
i'm trying to index an mp3 file with only one ID3 frame. using CLucene and TagLib. the following code works fine:
...
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);
}
...
but this one makes the application crash:
...
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);
}
...
why is that?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个范围问题 - 当您调用 writer->addDocument 时,您添加到其中的字段将被释放。请改用此代码:
您可能需要查看 cl_demo 和 cl_test 以查看一些代码示例。
This is a scope issue - by the time you call writer->addDocument, the fields you added to it are freed. Use this code instead:
You may want to look at cl_demo and cl_test to see some code samples.
您不需要为您添加的每个标签构造一个新的 lucene::document::Field 吗?看起来您为此重复使用了相同的地址,这是有问题的。我想调试器可以告诉你更多信息。
Don't you need to construct a new lucene::document::Field per tag you are adding? It seems like you are reusing the same address for this, which is problematic. I guess a debugger could tell you more.