CLucene 中的内存泄漏

Memory leak in CLucene

本文关键字:泄漏 内存 CLucene      更新时间:2023-10-16

我正在使用CLucene来创建索引和搜索。创建的索引文件大于 5 GB。我已经为CLucene搜索创建了单独的dll。DLL 构造函数包含以下代码

lucene::index::IndexReader ptrIndexReader;
lucene::search::IndexSearcher searcher; these are defined in class decalaration/
ptrIndexReader = IndexReader::open(pDir.c_str(),false,NULL);
searcher = new IndexSearcher(ptrIndexReader);

我使用一个搜索函数,其代码如下

bool LuceneWrapper::SearchIndex(wstring somevalue)
{
    lucene::analysis::KeywordAnalyzer fAnalyzer;
    Document doc = NULL;
    Hits hits = NULL;
    Query f_objQuery = NULL;
    NistRecord *f_objRecords = NULL;
    bool flag = false;
    try{
       if (ptrIndexReader == NULL)
       {
          return NULL;
       }
       // Initialize IndexSearcher
       wstring strQuery = _T("+somename:") + somevalue;
       // Initialize Query Parser, with Keyword Analyzer
       QueryParser *parser = new QueryParser( _T(""),&fAnalyzer);
       // Parse Query string
       f_objQuery = parser->parse(strQuery.c_str());
       // Search Index directory
       hits = searcher->search(f_objQuery);
       //searcher.
       int intHitCount =   0;
       intHitCount  = hits->length; 
       if(intHitCount > 0)
       {    
           if(doc!=NULL)
              delete [] doc;
           flag =  true;
       }
       //searcher.close();
  }
  catch(CLuceneError& objExp)
  {
      if(doc!=NULL)
          delete  doc;
      return false;
  }
  if(hits!=NULL)
      delete hits;
  if(f_objQuery!=NULL)
      delete f_objQuery;
  return flag ;
}

我正在搜索非常多的值。 根据记录计数,主内存会升高和升高,在级别上,它接近 2 GB,应用程序崩溃。谁能告诉我这有什么问题?为什么内存如此之高而应用程序崩溃?

你永远不会解除分配parser .

我看不出动态分配它的理由。
你为什么不直接说

 QueryParser parser( _T(""), &fAnalyzer);
 f_objQuery = parser.parse(strQuery.c_str());

您还需要确保在发生异常时同时删除f_objQueryhits
如果您可以访问它,std::unique_ptr可以在这里为您提供帮助。

(而且您不必对 NULL 进行太多测试 - 可以delete空指针。