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

Re: Why does this program segfault?



On Tue, Jan 15, 2002 at 11:02:10PM -0500, Jaldhar H. Vyas wrote:
> > free(NULL) is fine;
> 
> Well, strictly speaking it is calling delete which in the GNU C++
> implementation ends up calling free.  I seem to remember something about a

Yep; delete has the same semantic.  (Actually, it's delete [] here.)

(I can't stand seeing destructors freeing a dozen variables, but taking
three times as many lines with 
if(x)
   delete(x);

if(y)
   delete(y);
No need.)   

> C++ idiom being defining a class which is meant to be subclassed with a
> virtual destructor so if the base is used by itself, this kind of
> thing happens on purpose.  Or something, I use C++ too infrequently to
> fully master such a hairy language.

That would be a pure virtual, I think, and would produce compile-time
errors.
 
> What I've been able to gather from the comments in the source is that
> libapt-pkg does fancy things with mmap(2)  so it's not all straightforward
> pointers and structs.

Well, ideally, that shouldn't affect the interface at all.  One thing
that's not clear is when you pass a pkgCache * to pkgDepCache's constructor,
does it consider itself the owner of that pointer?  If so, it'll delete it,
so you can't pass a pointer that'll be freed elsewhere (or an auto.)

-- 
Glenn Maynard



Reply to: