2010-06-30 65 views
1

我試圖索引一個MP3文件只有一個ID3幀。使用CLucene和TagLib。下面的代碼工作正常:將文檔添加到Lucene索引導致崩潰

... 
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); 
} 
... 

這是爲什麼?

回答

2

這是一個範圍問題 - 在您調用writer-> addDocument時,您添加到其中的字段被釋放。改爲使用此代碼:

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

您可能想要查看cl_demo和cl_test以查看一些代碼示例。

+0

它工作[沒有第一個分號],但我不明白爲什麼字段被快速釋放;-)無論如何,謝謝你! – theorist 2010-07-01 15:37:25

+0

正如我所說,範圍:http://msdn.microsoft.com/en-us/library/b7kfh662%28VS.80%29.aspx。別客氣。 – synhershko 2010-07-01 15:51:43

0

您是否需要爲每個要添加的標記構造一個新的lucene :: document :: Field?似乎你正在重複使用相同的地址,這是有問題的。我想調試器可以告訴你更多。

+0

即使它有問題該字段不會在這種情況下重用,因爲for運算符的主體執行一次[一個ID3幀] – theorist 2010-07-01 14:01:36