Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In g2_addgrid.c, there is always a memory leak when a grid with extended data is used #178

Closed
edwardhartnett opened this issue Nov 2, 2021 · 0 comments · Fixed by #177
Closed
Assignees
Labels
bug Something isn't working

Comments

@edwardhartnett
Copy link
Contributor

In g2_addgrid() we have this code:

        if (mapgrid->needext)
        {
            free(mapgrid);
            mapgrid = extgridtemplate(igds[4], igdstmpl);
        }

This causes extra memory to be allocated in gridtemplates.c:

    if (number == 120)
    {
        new->extlen = list[1] * 2;
        new->ext = malloc(sizeof(g2int) * new->extlen);

Note that the ext pointer now has memory allocated. But when it's time to free this data, there is no call to free the ext memory (from g2_addgrid.c):

    /* Pack template extension, if appropriate. */
    j = mapgrid->maplen;
    if (mapgrid->needext && mapgrid->extlen > 0)
    {
        for (i = 0; i < mapgrid->extlen; i++)
        {
            nbits = abs(mapgrid->ext[i]) * 8;
            if (mapgrid->ext[i] >= 0 || igdstmpl[j] >= 0)
                sbit(cgrib, igdstmpl + j, iofst, nbits);
            else
            {
                sbit(cgrib, &one, iofst, 1);
                temp = abs(igdstmpl[j]);
                sbit(cgrib, &temp, iofst + 1, nbits - 1);
            }
            iofst = iofst + nbits;
            j++;
        }
    }
    free(mapgrid);

Note that mapgrid is freed, but mapgrid->ext needs to be freed first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant