Menu

#250 Memory leak in layer->name

v1.0 (example)
open
nobody
None
5
2019-10-31
2019-10-31
Eyal
No

valgrind found a memory leak around the layer name. In gerber.c, there is this line:

state->layer->name = gerb_fgetstring(fd, '*');

There is no corresponding free.

The fix is to call g_free in the destroy function. Also, because the data is not initialized, it's best to set it to NULL when creating a layer so that a layer without a name will not have some garbage value in the name. g_free(NULL) is a no-op.

Discussion

  • Eyal

    Eyal - 2019-10-31

    Here's a fix, tested in valgrind to show that it solves the problem. g_free(NULL) was also tested to be safe and the setting of layer->name = NULL was also tested to be necessary.

     
  • Eyal

    Eyal - 2019-10-31
    diff --git a/src/gerb_image.c b/src/gerb_image.c
    index 5e66e73..88458e6 100644
    --- a/src/gerb_image.c
    +++ b/src/gerb_image.c
    @@ -173,6 +173,7 @@ gerbv_destroy_image(gerbv_image_t *image)
            gerbv_layer_t *tempLayer = layer;
    
            layer = layer->next;
    
    +       g_free (tempLayer->name);
            g_free (tempLayer);
         }
         for (state = image->states; state != NULL; ) {
    @@ -312,6 +313,7 @@ gerbv_image_return_new_layer (gerbv_layer_t *previousLayer)
         previousLayer->next = newLayer;
         /* clear this boolean so we only draw the knockout once */
         newLayer->knockout.firstInstance = FALSE;
    +    newLayer->name = NULL;
         newLayer->next = NULL;
    
         return newLayer;
    
     

    Last edit: Eyal 2019-10-31

Log in to post a comment.

MongoDB Logo MongoDB