GRIB-774: Leak in example

This commit is contained in:
Shahram Najm 2015-06-01 14:27:47 +01:00
parent 22b7c322e4
commit eff8dc5090
1 changed files with 24 additions and 25 deletions

View File

@ -13,10 +13,9 @@
* *
* Description: * Description:
* Example on how to use keys_iterator functions and the * Example on how to use keys_iterator functions and the
* grib_keys_iterator structure to get all the available * codes_keys_iterator structure to get all the available
* keys in a GRIB message. * keys in a GRIB message.
* *
*
*/ */
#include <assert.h> #include <assert.h>
@ -25,7 +24,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include "grib_api.h" #include "eccodes.h"
#define MAX_KEY_LEN 255 #define MAX_KEY_LEN 255
#define MAX_VAL_LEN 1024 #define MAX_VAL_LEN 1024
@ -35,22 +34,22 @@ static void usage(char* progname);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
/* To skip read only and not coded keys /* To skip read only and not coded keys
unsigned long key_iterator_filter_flags=GRIB_KEYS_ITERATOR_SKIP_READ_ONLY || unsigned long key_iterator_filter_flags=CODES_KEYS_ITERATOR_SKIP_READ_ONLY ||
GRIB_KEYS_ITERATOR_SKIP_COMPUTED; CODES_KEYS_ITERATOR_SKIP_COMPUTED;
*/ */
unsigned long key_iterator_filter_flags=GRIB_KEYS_ITERATOR_ALL_KEYS; unsigned long key_iterator_filter_flags=CODES_KEYS_ITERATOR_ALL_KEYS;
/* valid name_spaces are ls and mars */ /* Choose a namespace. E.g. "ls", "time", "parameter", "geography", "statistics" */
char* name_space="ls"; char* name_space="ls";
/* name_space=NULL to get all the keys */ /* name_space=NULL to get all the keys */
/* char* name_space=0; */ /* char* name_space=0; */
FILE* f; FILE* f = NULL;
grib_handle* h=NULL; codes_handle* h=NULL;
grib_keys_iterator* kiter=NULL;
int err=0; int err=0;
int grib_count=0; int msg_count=0;
char value[MAX_VAL_LEN]; char value[MAX_VAL_LEN];
size_t vlen=MAX_VAL_LEN; size_t vlen=MAX_VAL_LEN;
@ -63,40 +62,40 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
while((h = grib_handle_new_from_file(0,f,&err)) != NULL) { while((h = grib_handle_new_from_file(0,f,&err)) != NULL)
{
grib_count++; codes_keys_iterator* kiter=NULL;
/*printf("-- GRIB N. %d --\n",grib_count);*/ msg_count++;
/*printf("-- GRIB N. %d --\n",msg_count);*/
if(!h) { if(!h) {
printf("ERROR: Unable to create grib handle\n"); printf("ERROR: Unable to create grib handle\n");
exit(1); exit(1);
} }
kiter=grib_keys_iterator_new(h,key_iterator_filter_flags,name_space); kiter=codes_keys_iterator_new(h,key_iterator_filter_flags,name_space);
if (!kiter) { if (!kiter) {
printf("ERROR: Unable to create keys iterator\n"); printf("ERROR: Unable to create keys iterator\n");
exit(1); exit(1);
} }
while(grib_keys_iterator_next(kiter)) while(codes_keys_iterator_next(kiter))
{ {
const char* name = grib_keys_iterator_get_name(kiter); const char* name = codes_keys_iterator_get_name(kiter);
vlen=MAX_VAL_LEN; vlen=MAX_VAL_LEN;
bzero(value,vlen); bzero(value,vlen);
GRIB_CHECK(grib_get_string(h,name,value,&vlen),name); CODES_CHECK(codes_get_string(h,name,value,&vlen),name);
printf("%s = %s\n",name,value); printf("%s = %s\n",name,value);
} }
grib_keys_iterator_delete(kiter); codes_keys_iterator_delete(kiter);
codes_handle_delete(h);
} }
fclose(f);
return 0; return 0;
} }
static void usage(char* progname) { static void usage(char* progname)
{
printf("\nUsage: %s grib_file\n",progname); printf("\nUsage: %s grib_file\n",progname);
exit(1); exit(1);
} }