开发者

adding a document to a Lucene index causes crash

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->add开发者_JAVA百科Document(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?!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜