[asterisk-dev] ast_frame allocation/free question

Russell Bryant russell at digium.com
Tue Nov 27 10:17:32 CST 2007


Sergio Garcia Murillo wrote:
> I've been trying to fix some memory leaks in my applications related 
> to frame allocation problems.
> If I malloc a frame in an application and set the mallocd to AST_MALLOCD_HDR
> it seems that ast_frfree is not called nowhere after writing it with ast_write.
> 
> I have checked and ast_write don't call ast_frfree for the input frame. In
> case of a video frame just calls  chan->tech->write(chan, fr) but in rtp.c it's 
> not freed also.

The code is correct.  ast_write() should not free the frame.  It is the caller
of ast_write() who must free the frame.  In the most general case, this is done
in the bridging functions such ast ast_generic_bridge() in channel.c.  It reads
a frame using ast_read(), writes it to the other channel with ast_write(), and
then frees it.

> I've solved the problem in some cases making the frame static and in oder cases
> setting mallocd to 0 and freeing it withing the application after using it.

You certainly should use a single frame structure over and over whenever
possible, as it is more efficient.  If you must allocate a frame, ast_frdup()
may do what you need.  If that doesn't do it, then you should probably expose
ast_frame_header_new() from frame.c, as it takes advantage of cached frame headers.

-- 
Russell Bryant
Senior Software Engineer
Open Source Team Lead
Digium, Inc.



More information about the asterisk-dev mailing list