[FFmpeg-user] How to correctly free a double-linked list

Ulf Zibis Ulf.Zibis at gmx.de
Sun Jul 7 15:20:41 EEST 2019


Am 07.07.19 um 13:51 schrieb Paul B Mahol:
> There is av_calloc, it calls memset for you after allocation
Thanks for the hint. I now have:
    if (s->report >= R_SHIFTS) {
        s->shifts_sums = av_malloc((s->lines + 1) *
sizeof(*s->shifts_sums));
        for (int l = s->lines; l >= 0; l--)
            s->shifts_sums[l] = av_calloc(s->span_r - s->span_l + 1,
sizeof(**s->shifts_sums));
    }

> It looks like you access unallocated array in uninit because your
> allocation is under condition.
You mean this? :
    if (s->report >= R_SHIFTS)
It's always the same, so it should always hold.

> Before freeing lines you need to check if array is not NULL.
OK, now I have:
        av_log(ctx, AV_LOG_WARNING, "now start freeing shifts_sums ...\n");
        for (int l = 0; l <= s->lines; l++) {
            av_log(ctx, AV_LOG_WARNING, "freeing line %d ...\n", l);
            if (s->shifts_sums[l])
                av_freep(&(s->shifts_sums[l]));
        }
        av_log(ctx, AV_LOG_WARNING, "lines of shifts_sums freed!\n");
        av_freep(&(s->shifts_sums));

        av_log(ctx, AV_LOG_WARNING, "shifts_sums freed!\n");

Unfortunately it doesn't help :-(


> You need to memset/av_calloc array after allocation that hold pointers to lines.
> Otherwise you use uninitialized memory.
I'm not sure if I understand correctly. Isn't this enough initialization? :
        for (int l = s->lines; l >= 0; l--)
            s->shifts_sums[l] = av_calloc(s->span_r - s->span_l + 1,
sizeof(**s->shifts_sums));
I don't get, why I first should write NULLs in there.

> If you can, install and use valgrind to help you debug such problems.

Good idea, I will do that ... guess it needs some time to understand how
it works.

-Ulf




More information about the ffmpeg-user mailing list