ECC-424: codes_bufr_keys_iterator fix memory leaks

This commit is contained in:
Shahram Najm 2017-02-06 10:33:55 +00:00
parent 36f37e0fe5
commit 8ca2189927
3 changed files with 31 additions and 6 deletions

View File

@ -174,6 +174,7 @@ char* codes_bufr_keys_iterator_get_name(bufr_keys_iterator* kiter)
int *r=0;
char* ret=0;
Assert(kiter->current);
grib_context* c = kiter->handle->context;
if (kiter->prefix) {
int iattribute=kiter->i_curr_attribute-1;
@ -189,6 +190,18 @@ char* codes_bufr_keys_iterator_get_name(bufr_keys_iterator* kiter)
sprintf(ret,"%s",kiter->current->name);
}
}
/* Store in list of names to be deleted later */
grib_string_list* sl=(grib_string_list*)grib_context_malloc_clear(c, sizeof(grib_string_list));
sl->value = ret;
if (!kiter->names) {
kiter->names = sl;
} else {
/*Add to end of linked list*/
grib_string_list* q = kiter->names;
while(q->next) q=q->next;
q->next = sl;
}
return ret;
}
@ -200,11 +213,18 @@ grib_accessor* codes_bufr_keys_iterator_get_accessor(bufr_keys_iterator* kiter)
int codes_bufr_keys_iterator_delete(bufr_keys_iterator* kiter)
{
if (kiter) {
grib_context* c = kiter->handle->context;
grib_string_list* sl = kiter->names;
while(sl) {
grib_string_list* n = sl->next;
grib_context_free(c, sl->value);
grib_context_free(c, sl);
sl = n;
}
kiter->names=NULL;
if(kiter->seen)
grib_trie_delete(kiter->seen);
/*if (kiter->name_space)
grib_context_free(kiter->handle->context,kiter->name_space);*/
grib_context_free(kiter->handle->context,kiter);
grib_context_free(c,kiter);
}
return 0;
}

View File

@ -76,7 +76,11 @@ char** codes_bufr_copy_data_return_copied_keys(grib_handle* hin,grib_handle* hou
cannot be copied because is not in the output handle
*/
*err=codes_copy_key(hin, hout, name, 0);
if (*err==0) k=grib_sarray_push(hin->context, k, name);
if (*err==0) {
/* 'name' will be freed when we call codes_bufr_keys_iterator_delete so copy */
char* copied_name = strdup(name);
k=grib_sarray_push(hin->context, k, copied_name);
}
}
*nkeys=grib_sarray_used_size(k);
keys=grib_sarray_get_array(hin->context, k);
@ -113,7 +117,6 @@ int codes_bufr_copy_data(grib_handle* hin, grib_handle* hout)
*/
err=codes_copy_key(hin, hout, name, 0);
if (err==0) nkeys++;
grib_context_free(hin->context,name);
}
if (nkeys > 0) {

View File

@ -317,7 +317,9 @@ int grib_write_message(grib_handle* h,const char* file,const char* mode)
grib_handle* grib_handle_clone ( grib_handle* h )
{
return grib_handle_new_from_message_copy ( h->context, h->buffer->data, h->buffer->ulength );
grib_handle* result = grib_handle_new_from_message_copy ( h->context, h->buffer->data, h->buffer->ulength );
result->product_kind = h->product_kind;
return result;
}
grib_handle* codes_handle_new_from_file(grib_context* c, FILE* f, ProductKind product, int* error)