[asterisk-dev] [Code Review] automagically set a pointer to null after it is freed with ast_free

Tony Mountifield tony at softins.co.uk
Tue Sep 20 10:44:14 CDT 2011


In article <1316530179.4192.20.camel at solo.digium.internal>,
Matthew Nicholson <mnicholson at digium.com> wrote:
> On Fri, 2011-09-16 at 15:13 +0000, Tony Mountifield wrote:
> > I would NEVER call free() when I didn't know whether the pointer
> > contained NULL or not!
> 
> There are some structures that have members that are initialized to NULL
> when the structure is allocated and possibly initialized to something
> else later. When it is time to clean them up, they are known to either
> be NULL or a valid pointer that needs to be freed. The NULL check in
> ast_free() allows the caller to just call ast_free() without checking
> for NULL. This is desirable behavior.

This is fair enough, but it actually supports the original proponent's
point, which is to enforce what you have just stated: a pointer that is
intended to point to a dynamically allocated item should contain either
NULL, or a *valid pointer that needs to be freed*.  Not a stale pointer
that has already been freed.

This will be my last comment on the topic, as it's evidently an issue
of programming style on which people hold different opinions!

Cheers
Tony
-- 
Tony Mountifield
Work: tony at softins.co.uk - http://www.softins.co.uk
Play: tony at mountifield.org - http://tony.mountifield.org



More information about the asterisk-dev mailing list