[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Why does this program segfault?



Is it because pkgDepCache has a virtual destructor?  If so am I supposed
to subclass it or am I just using it the wrong way?  (Sorry, the the
intricacies of C++ are a bit hazy for me and libapt-pkg is practically
undocumented.)

/*
 * Compile it like this:
 * $ c++ -O2 -Wall -s -o showpackages showpackages.cc -lapt-pkg
 */

#include <iostream>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/init.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/progress.h>

int main ()
{
  if (!pkgInitConfig(*_config) || !pkgInitSystem(*_config, _system))
  {
    std::cerr << "Could not initialize apt library." << endl;
    std::exit(1);
  }

  _config->Set("quiet", 2);

  pkgCacheFile cachefile;
  OpProgress progress;
  if (!cachefile.Open(progress, false))
  {
    std::cerr << "Could not open cache." << endl;
    std::exit(1);
  }

  pkgDepCache cache = *cachefile;
  for (pkgCache::PkgIterator it = cache.PkgBegin(); !it.end(); it++)
  {
    if (it->CurrentState == pkgCache::State::Installed)
    {
      std::cout << it.Name() << ' ' << it.CurrentVer().VerStr() << endl;
    }
  }

  cachefile.Close();

  return 0;
}

-- 
Jaldhar H. Vyas <jaldhar@debian.org>
It's a girl! See the pictures - http://www.braincells.com/shailaja/




Reply to: