views:

60

answers:

2

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?!

A: 

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.

Yuval F
even if it's problematic the field isn't reused in this case since the body of the for operator is executed once [one ID3 frame]
theorist
+1  A: 

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.

synhershko
it worked [without the first semicolon], but i don't understand why the fields are freed so quickly ;-) thank you anyway!
theorist
As I said, scopes: http://msdn.microsoft.com/en-us/library/b7kfh662%28VS.80%29.aspx. You're welcome.
synhershko