Memory leaks: grib_dumper_class. Use context directly rather than via handle

This commit is contained in:
Shahram Najm 2020-05-22 19:43:38 +01:00
parent df441ee764
commit 5478d10505
18 changed files with 58 additions and 55 deletions

View File

@ -650,7 +650,7 @@ struct grib_dumper
void* arg;
int depth;
long count;
grib_handle* handle;
grib_context* context;
grib_dumper_class* cclass;
};

View File

@ -67,7 +67,7 @@ void grib_init_dumper(grib_dumper* d)
void grib_dumper_delete(grib_dumper* d)
{
grib_dumper_class* c = d->cclass;
grib_context* ctx = d->handle->context;
grib_context* ctx = d->context;
while (c) {
grib_dumper_class* s = c->super ? *(c->super) : NULL;
if (c->destroy)

View File

@ -36,7 +36,7 @@ grib_dumper* grib_dumper_factory(const char* op, const grib_handle* h, FILE* out
grib_dumper_class* c = *(table[i].cclass);
grib_dumper* d = (grib_dumper*)grib_context_malloc_clear(h->context, c->size);
d->depth = 0;
d->handle = (grib_handle*)h;
d->context = h->context;
d->cclass = c;
d->option_flags = option_flags;
d->arg = arg;
@ -100,12 +100,14 @@ void grib_dump_keys(grib_handle* h, FILE* f, const char* mode, unsigned long fla
grib_dumper_delete(dumper);
}
/* Note: if the dumper passed in is non-NULL, it will be freed up */
grib_dumper* grib_dump_content_with_dumper(grib_handle* h, grib_dumper* dumper, FILE* f, const char* mode, unsigned long flags, void* data)
{
long count = 1;
if (dumper != NULL) {
count = dumper->count;
count++;
grib_dumper_delete(dumper);
}
dumper = grib_dumper_factory(mode ? mode : "serialize", h, f, flags, data);
if (!dumper)

View File

@ -124,7 +124,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_decode_C* self = (grib_dumper_bufr_decode_C*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
d->count = 1;
@ -140,7 +140,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_decode_C* self = (grib_dumper_bufr_decode_C*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;

View File

@ -123,7 +123,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_decode_filter* self = (grib_dumper_bufr_decode_filter*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
self->isLeaf = 0;
@ -138,7 +138,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_decode_filter* self = (grib_dumper_bufr_decode_filter*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;

View File

@ -124,7 +124,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_decode_fortran* self = (grib_dumper_bufr_decode_fortran*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
d->count = 1;
@ -140,7 +140,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_decode_fortran* self = (grib_dumper_bufr_decode_fortran*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;

View File

@ -124,7 +124,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_decode_python* self = (grib_dumper_bufr_decode_python*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
d->count = 1;
@ -140,7 +140,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_decode_python* self = (grib_dumper_bufr_decode_python*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;

View File

@ -124,7 +124,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_encode_C* self = (grib_dumper_bufr_encode_C*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
d->count = 1;
@ -140,7 +140,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_encode_C* self = (grib_dumper_bufr_encode_C*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;

View File

@ -125,7 +125,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_encode_filter* self = (grib_dumper_bufr_encode_filter*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
self->isLeaf = 0;
@ -140,7 +140,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_encode_filter* self = (grib_dumper_bufr_encode_filter*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;
@ -613,7 +613,7 @@ static void dump_string_array(grib_dumper* d, grib_accessor* a, const char* comm
grib_context_free(c, prefix);
depth -= 2;
}
for (i=0; i<size; ++i) grib_context_free(c, values[i]);
grib_context_free(c, values);
(void)err; /* TODO */
}

View File

@ -124,7 +124,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_encode_fortran* self = (grib_dumper_bufr_encode_fortran*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
d->count = 1;
@ -140,7 +140,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_encode_fortran* self = (grib_dumper_bufr_encode_fortran*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;
@ -727,7 +727,7 @@ static void dump_string_array(grib_dumper* d, grib_accessor* a, const char* comm
grib_context_free(c, prefix);
depth -= 2;
}
for (i=0; i<size; ++i) grib_context_free(c, values[i]);
grib_context_free(c, values);
(void)err; /* TODO */
}

View File

@ -124,7 +124,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_encode_python* self = (grib_dumper_bufr_encode_python*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
d->count = 1;
@ -140,7 +140,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_encode_python* self = (grib_dumper_bufr_encode_python*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;
@ -671,7 +671,7 @@ static void dump_string_array(grib_dumper* d, grib_accessor* a, const char* comm
grib_context_free(c, prefix);
depth -= 2;
}
for (i=0; i<size; ++i) grib_context_free(c, values[i]);
grib_context_free(c, values);
(void)err; /* TODO */
}

View File

@ -121,7 +121,7 @@ static void init_class(grib_dumper_class* c) {}
static int init(grib_dumper* d)
{
grib_dumper_bufr_simple* self = (grib_dumper_bufr_simple*)d;
grib_context* c = d->handle->context;
grib_context* c = d->context;
self->section_offset = 0;
self->empty = 1;
self->isLeaf = 0;
@ -137,7 +137,7 @@ static int destroy(grib_dumper* d)
grib_dumper_bufr_simple* self = (grib_dumper_bufr_simple*)d;
grib_string_list* next = self->keys;
grib_string_list* cur = NULL;
grib_context* c = d->handle->context;
grib_context* c = d->context;
while (next) {
cur = next;
next = next->next;
@ -623,7 +623,7 @@ static void dump_string_array(grib_dumper* d, grib_accessor* a, const char* comm
if (dofree)
grib_context_free(c, prefix);
}
for (i=0; i<size; ++i) grib_context_free(c, values[i]);
grib_context_free(c, values);
(void)err; /* TODO */
}

View File

@ -328,7 +328,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
int i, k, err = 0;
int more = 0;
size_t size = a->length;
unsigned char* buf = (unsigned char*)grib_context_malloc(d->handle->context, size);
unsigned char* buf = (unsigned char*)grib_context_malloc(d->context, size);
if (a->length == 0 &&
(d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
@ -354,7 +354,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
err = grib_unpack_bytes(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " *** ERR=%d (%s) [grib_dumper_debug::dump_bytes]\n}", err, grib_get_error_message(err));
return;
}
@ -387,7 +387,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
for (i = 0; i < d->depth; i++)
fprintf(self->dumper.out, " ");
fprintf(self->dumper.out, "} # %s %s \n", a->creator->op, a->name);
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_values(grib_dumper* d, grib_accessor* a)
@ -409,7 +409,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
dump_double(d, a, NULL);
return;
}
buf = (double*)grib_context_malloc_clear(d->handle->context, size * sizeof(double));
buf = (double*)grib_context_malloc_clear(d->context, size * sizeof(double));
set_begin_end(d, a);
@ -431,7 +431,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
err = grib_unpack_double(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " *** ERR=%d (%s) [grib_dumper_debug::dump_values]\n}", err, grib_get_error_message(err));
return;
}
@ -468,7 +468,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
for (i = 0; i < d->depth; i++)
fprintf(self->dumper.out, " ");
fprintf(self->dumper.out, "} # %s %s \n", a->creator->op, a->name);
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_label(grib_dumper* d, grib_accessor* a, const char* comment)

View File

@ -473,7 +473,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
int i,k,err =0;
int more = 0;
size_t size = a->length;
unsigned char* buf = grib_context_malloc(d->handle->context,size);
unsigned char* buf = grib_context_malloc(d->context,size);
if ( (a->flags & GRIB_ACCESSOR_FLAG_DUMP) == 0)
return;
@ -504,7 +504,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
err = grib_unpack_bytes(a,buf,&size);
if(err){
grib_context_free(d->handle->context,buf);
grib_context_free(d->context,buf);
fprintf(self->dumper.out," *** ERR=%d (%s) [grib_dumper_default::dump_bytes]\n}",err,grib_get_error_message(err));
return ;
}
@ -537,7 +537,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");
fprintf(self->dumper.out,"} # %s %s \n",a->creator->op, a->name);
grib_context_free(d->handle->context,buf);
grib_context_free(d->context,buf);
#endif
}
@ -560,7 +560,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
return;
}
buf = (double*)grib_context_malloc(d->handle->context, size * sizeof(double));
buf = (double*)grib_context_malloc(d->context, size * sizeof(double));
print_offset(self->dumper.out, d, a);
@ -603,7 +603,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
err = grib_unpack_double(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " *** ERR=%d (%s) [grib_dumper_default::dump_values]\n}", err, grib_get_error_message(err));
return;
}
@ -637,7 +637,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
fprintf(self->dumper.out, " ");
fprintf(self->dumper.out, "} \n");
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_label(grib_dumper* d, grib_accessor* a, const char* comment)

View File

@ -261,7 +261,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
if (size == 0)
return;
buf = (unsigned char*)grib_context_malloc(d->handle->context, size);
buf = (unsigned char*)grib_context_malloc(d->context, size);
if (!buf) {
fprintf(self->dumper.out, "/* %s: cannot malloc(%ld) */\n", a->name, (long)size);
@ -271,7 +271,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
err = grib_unpack_bytes(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " *** ERR=%d (%s) [grib_dumper_grib_encode_C::dump_bytes]\n}", err, grib_get_error_message(err));
return;
}
@ -297,7 +297,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
fprintf(self->dumper.out,"\n");
}
#endif
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_values(grib_dumper* d, grib_accessor* a)
@ -335,7 +335,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
return;
}
buf = (double*)grib_context_malloc(d->handle->context, size * sizeof(double));
buf = (double*)grib_context_malloc(d->context, size * sizeof(double));
if (!buf) {
fprintf(self->dumper.out, "/* %s: cannot malloc(%ld) */\n", a->name, (long)size);
return;
@ -344,7 +344,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
err = grib_unpack_double(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " /* Error accessing %s (%s) */", a->name, grib_get_error_message(err));
return;
}
@ -371,7 +371,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
fprintf(self->dumper.out, " GRIB_CHECK(grib_set_%s_array(h,\"%s\",v%s,size),%d);\n", stype, a->name, stype, 0);
fprintf(self->dumper.out, " free(v%s);\n", stype);
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_label(grib_dumper* d, grib_accessor* a, const char* comment)

View File

@ -246,7 +246,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
int i, k, err = 0;
int more = 0;
size_t size = a->length;
unsigned char* buf = (unsigned char*)grib_context_malloc(d->handle->context, size);
unsigned char* buf = (unsigned char*)grib_context_malloc(d->context, size);
if ((a->flags & GRIB_ACCESSOR_FLAG_HIDDEN) != 0)
return;
@ -271,7 +271,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
err = grib_unpack_bytes(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " *** ERR=%d (%s) [grib_dumper_serialize::dump_bytes]\n}", err, grib_get_error_message(err));
return;
}
@ -304,7 +304,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
for (i = 0; i < d->depth; i++)
fprintf(self->dumper.out, " ");
fprintf(self->dumper.out, "} # %s %s \n", a->creator->op, a->name);
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_values(grib_dumper* d, grib_accessor* a)
@ -370,7 +370,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
if ((d->option_flags & GRIB_DUMP_FLAG_VALUES) == 0)
return;
buf = (double*)grib_context_malloc(d->handle->context, size * sizeof(double));
buf = (double*)grib_context_malloc(d->context, size * sizeof(double));
fprintf(self->dumper.out, "%s (%ld) {", a->name, (long)size);
@ -387,7 +387,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
err = grib_unpack_double(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " *** ERR=%d (%s) [grib_dumper_serialize::dump_values]\n}", err, grib_get_error_message(err));
return;
}
@ -403,7 +403,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
fprintf(self->dumper.out, "\n");
}
fprintf(self->dumper.out, "}\n");
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_label(grib_dumper* d, grib_accessor* a, const char* comment)

View File

@ -345,7 +345,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
int i, k, err = 0;
int more = 0;
size_t size = a->length;
unsigned char* buf = (unsigned char*)grib_context_malloc(d->handle->context, size);
unsigned char* buf = (unsigned char*)grib_context_malloc(d->context, size);
if (a->length == 0 &&
(d->option_flags & GRIB_DUMP_FLAG_CODED) != 0)
@ -376,7 +376,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
err = grib_unpack_bytes(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " *** ERR=%d (%s) [grib_dumper_wmo::dump_bytes]\n}", err, grib_get_error_message(err));
return;
}
@ -409,7 +409,7 @@ static void dump_bytes(grib_dumper* d, grib_accessor* a, const char* comment)
for (i = 0; i < d->depth; i++)
fprintf(self->dumper.out, " ");
fprintf(self->dumper.out, "} # %s %s \n", a->creator->op, a->name);
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_values(grib_dumper* d, grib_accessor* a)
@ -433,7 +433,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
dump_double(d, a, NULL);
return;
}
buf = (double*)grib_context_malloc(d->handle->context, size * sizeof(double));
buf = (double*)grib_context_malloc(d->context, size * sizeof(double));
set_begin_end(d, a);
@ -464,7 +464,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
err = grib_unpack_double(a, buf, &size);
if (err) {
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
fprintf(self->dumper.out, " *** ERR=%d (%s) [grib_dumper_wmo::dump_values]\n}", err, grib_get_error_message(err));
return;
}
@ -504,7 +504,7 @@ static void dump_values(grib_dumper* d, grib_accessor* a)
/*for(i = 0; i < d->depth ; i++) fprintf(self->dumper.out," ");*/
fprintf(self->dumper.out, "} # %s %s \n", a->creator->op, a->name);
grib_context_free(d->handle->context, buf);
grib_context_free(d->context, buf);
}
static void dump_label(grib_dumper* d, grib_accessor* a, const char* comment)

View File

@ -533,6 +533,7 @@ int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h)
}
}
print_header(options);
/* Note: if the dumper passed in is non-NULL, it will be freed up */
dumper = grib_dump_content_with_dumper(h, dumper, stdout, dumper_name, options->dump_flags, 0);
if (!dumper)
exit(1);