From 9eaab71e99b1fedd8b2a29a99b8512b01719469a Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Fri, 3 Jul 2009 00:27:05 -0700 Subject: Fixed bugs and memory leaks. --- Makefile | 4 ++-- upb_context.c | 20 ++++++++++++++------ upb_msg.c | 4 +++- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 7a98b7e..ced605c 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,11 @@ CC=gcc CXX=g++ CFLAGS=-std=c99 -CPPFLAGS=-Wall -Wextra -pedantic -g -DUPB_UNALIGNED_READS_OK -fomit-frame-pointer +CPPFLAGS=-O0 -Wall -Wextra -pedantic -g -DUPB_UNALIGNED_READS_OK -fomit-frame-pointer OBJ=upb_parse.o upb_table.o upb_msg.o upb_context.o descriptor.o all: $(OBJ) test_table tests clean: - rm -f $(OBJ) tests + rm -f *.o test_table tests libupb.a: $(OBJ) ar rcs libupb.a $(OBJ) diff --git a/upb_context.c b/upb_context.c index 702358a..e9cfdfb 100644 --- a/upb_context.c +++ b/upb_context.c @@ -34,6 +34,16 @@ bool upb_context_init(struct upb_context *c) void upb_context_free(struct upb_context *c) { + struct upb_symtab_entry *e = upb_strtable_begin(&c->symtab); + for(; e; e = upb_strtable_next(&c->symtab, &e->e)) { + switch(e->type) { + case UPB_SYM_MESSAGE: upb_msg_free(e->ref.msg); break; + case UPB_SYM_ENUM: upb_enum_free(e->ref._enum); break; + default: break; /* TODO */ + } + free(e->ref.msg); /* The pointer is the same for all. */ + free(e->e.key.ptr); + } upb_strtable_free(&c->symtab); for(size_t i = 0; i < c->fd_len; i++) free(c->fd[i]); free(c->fd); @@ -165,8 +175,6 @@ static bool insert_message(struct upb_strtable *t, return false; } upb_strtable_insert(t, &e.e); - printf("Inserted "); - upb_print(&e.e.key); /* Add nested messages and enums. */ //if(d->set_flags.has.nested_type) @@ -175,7 +183,8 @@ static bool insert_message(struct upb_strtable *t, if(!insert_message(t, d->nested_type->elements[i], &fqname)) return false; - if(d->set_flags.has.enum_type) + //if(d->set_flags.has.enum_type) + if(d->enum_type) for(unsigned int i = 0; i < d->enum_type->len; i++) if(!insert_enum(t, d->enum_type->elements[i], &fqname)) return false; @@ -228,9 +237,7 @@ bool upb_context_addfd(struct upb_context *c, ref = resolve2(&c->symtab, &tmp, &e->e.key, fd->type_name, UPB_SYM_ENUM); else continue; /* No resolving necessary. */ - upb_print(&e->e.key); - if(!ref.msg) { printf("FAILED!\n"); goto error; } /* Ref. to undefined symbol. */ - printf("Successful!\n"); + if(!ref.msg) goto error; /* Ref. to undefined symbol. */ upb_msg_ref(m, f, ref); } } @@ -240,6 +247,7 @@ bool upb_context_addfd(struct upb_context *c, for(e = upb_strtable_begin(&tmp); e; e = upb_strtable_next(&tmp, &e->e)) upb_strtable_insert(&c->symtab, &e->e); + upb_strtable_free(&tmp); return true; error: diff --git a/upb_msg.c b/upb_msg.c index 86c46de..d6b1594 100644 --- a/upb_msg.c +++ b/upb_msg.c @@ -16,7 +16,8 @@ static int div_round_up(int numerator, int denominator) { } static int compare_fields(const void *e1, const void *e2) { - const google_protobuf_FieldDescriptorProto *fd1 = e1, *fd2 = e2; + const google_protobuf_FieldDescriptorProto *fd1 = *(void**)e1; + const google_protobuf_FieldDescriptorProto *fd2 = *(void**)e2; /* Required fields go before non-required. */ bool req1 = fd1->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REQUIRED; bool req2 = fd2->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REQUIRED; @@ -90,6 +91,7 @@ void upb_msg_free(struct upb_msg *m) upb_inttable_free(&m->fields_by_num); upb_strtable_free(&m->fields_by_name); free(m->fields); + free(m->field_descriptors); } void *upb_msg_new(struct upb_msg *m) -- cgit v1.2.3