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

Re: Why does this program segfault?



By the way, Jaldhar, don't reply to a message to post a new thread on
mailing lists.  It gets your thread tagged as a subthread.

On Mon, Jan 14, 2002 at 07:35:55PM -0500, Matt Zimmerman wrote:
> >   pkgDepCache cache = *cachefile;
> >   for (pkgCache::PkgIterator it = cache.PkgBegin(); !it.end(); it++)

Unrelated nitpick: use ++it for iterators; it doesn't matter for most,
but it can be a lot faster for classes with non-trivial iteraters, so
it's a good habit.

> >   {
> >     if (it->CurrentState == pkgCache::State::Installed)
> >     {
> >       std::cout << it.Name() << ' ' << it.CurrentVer().VerStr() << endl;
> >     }
> >   }
> 
> This seems to be happening because pkgDepCache is trying to
> free(delLocalPolicy), which is initiailzed to 0.

free(NULL) is fine; 

       free() frees the memory space pointed to by ptr, which must have
       been returned by a previous call to malloc(), calloc()  or
       realloc().  Otherwise, or if free(ptr) has already been called before,
       undefined behaviour occurs.  If ptr is NULL, no operation is performed.

(Not that manpages are an authoratative source for libc documentation,
but this *is* the behavior on every platform I've tried it on.)

I don't know this library, but if it's being used right, it's a library
bug.  (If it's not, then it might still be a library bug; you're doing
an assignment that it allows, so it shouldn't explode.)

Something odd happens with "pkgDepCache cache = *cachefile;"; it works
with "pkgDepCache cache((pkgCache *)cachefile);".  Remove the cast and
you get an ambiguous overload error.  Probably something amiss with the
added casts in pkgCacheFile.

> I think what you want is pkgCache instead of pkgDepCache.  That will avoid
> the segfault and still do what you want.

(probably true, but it still shouldn't blow up)

-- 
Glenn Maynard



Reply to: