[asterisk-dev] ast_frame allocation/free question

Sergio Garcia Murillo sergio.garcia at fontventa.com
Tue Nov 27 12:48:54 CST 2007


----- Original Message ----- 
From: "Russell Bryant" <russell at digium.com>
To: "Asterisk Developers Mailing List" <asterisk-dev at lists.digium.com>
Sent: Tuesday, November 27, 2007 5:17 PM
Subject: Re: [asterisk-dev] ast_frame allocation/free question


> 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.
>

But it's not a bit incoherent? I mean, in many applications that comes with
asterisk I
can see the following:

        /* Do our thing here */
        while (ast_waitfor (chan, -1) > -1 && !h.hangup)
        {
                f = ast_read (chan);
                if (!f)
                        break;
                if (f->frametype == AST_FRAME_VOICE) {
                        sms_process (&h, f->samples, f->data);
                }

                ast_frfree (f);
        }

So if you're writting a custom application you have to take care for
deleting the
frames you write and the frames you read, but if you write a custom channel
you
don't have to worry?
Shouldn't be better to have a general policy about it, for example the one
that creates
the frame has to take care of deleting it, or the one that consumes the
frame is the one
that  deletes it. Just as an idea, as I said before I've just made
everything static and
fixed my problem.

BR
Sergio






More information about the asterisk-dev mailing list