Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4873

Memory leak in c_glib

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Duplicate
    • 0.11.0, 0.12.0, 0.13.0
    • 0.14.0
    • C glib - Compiler
    • None

    Description

      Using the following thrift definition, compiler generates code that is leaking memory:

      enum Command {                                                                  
              CMD_A = 0,                                                              
              CMD_B = 0,                                                              
              CMD_C = 0,                                                              
      }                                                                               
                                                                                      
      struct CommandRequest {                                                         
              1: string id;                                                           
              2: i32 type;                                                            
              3: list<Command> commands;                                              
      }  
      

      Since we use Enum element for the list, compiler uses GArray for that, like:

      struct _CommandRequest                                                          
      {                                                                               
        ThriftStruct parent;                                                          
                                                                                      
        /* public */                                                                  
        gchar * id;                                                                   
        gboolean __isset_id;                                                          
        gint32 type;                                                                  
        gboolean __isset_type;                                                        
        GArray * commands;                                                            
        gboolean __isset_commands;                                                    
      };                                                                              
      typedef struct _CommandRequest CommandRequest;
      

      However, when reading the object from stream, it allocates memory for every element that it reads:

                  for (i = 0; i < size; i++)                                          
                  {                                                                   
                    Command* _elem0 = g_new (Command, 1);                             
                    gint32 ecast1;                                                    
                    if ((ret = thrift_protocol_read_i32 (protocol, &ecast1, error)) < 0)
                      return -1;                                                      
                    xfer += ret;                                                      
                    *_elem0 = (Command)ecast1;                                        
                    g_array_append_vals (this_object->commands, _elem0, 1);           
                  } 
      

      Although when clearing this element with g_array_unref, this does not free memory.
      From Glib documentation we should set g_array_clear_func() if we want to free memory allocated for elements:

      If array contents point to dynamically-allocated memory, they should be freed separately if free_seg is TRUE and no clear_func function has been set for array .

      But I believe the best option would be just to read the elements into int and casting it to proper type, without allocating memory.

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            jensg Jens Geyer
            matejkupljen Matej Kupljen
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment