将文档添加到 Lucene 索引会导致崩溃

发布于 2024-09-07 07:10:12 字数 1631 浏览 3 评论 0原文

我正在尝试索引一个只有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝對不後悔。 2024-09-14 07:10:12

这是一个范围问题 - 当您调用 writer->addDocument 时,您添加到其中的字段将被释放。请改用此代码:

document->add(* new lucene::document::Field(fieldName, fieldValue, true, true, true, false));

您可能需要查看 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:

document->add(* new lucene::document::Field(fieldName, fieldValue, true, true, true, false));

You may want to look at cl_demo and cl_test to see some code samples.

极度宠爱 2024-09-14 07:10:12

您不需要为您添加的每个标签构造一个新的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文