Browse files

AVRO-1165. C: Fix memory leak in AVRO_LINK generic value implementations

Contributed by Vivek Nadkarni.


git-svn-id: https://svn.apache.org/repos/asf/avro/trunk@1386905 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 5f19b06 commit a2589652eea0a321df6a4b9d60c43dc7a828112c @dcreager dcreager committed Sep 17, 2012
Showing with 90 additions and 0 deletions.
  1. +3 −0 CHANGES.txt
  2. +4 −0 lang/c/src/generic.c
  3. +1 −0 lang/c/tests/CMakeLists.txt
  4. +82 −0 lang/c/tests/test_avro_1165.c
View
3 CHANGES.txt
@@ -86,6 +86,9 @@ Avro 1.7.2 (unreleased)
AVRO-1164. C: Clean up valgrind warnings in test_avro_schema test case.
(Vivek Nadkarni via dcreager)
+ AVRO-1165. C: Fix memory leak in generic value implementations involving
+ LINK schemas. (Vivek Nadkarni via dcreager)
+
Avro 1.7.1 (16 July 2012)
NEW FEATURES
View
4 lang/c/src/generic.c
@@ -184,6 +184,10 @@ avro_generic_link_decref_iface(avro_value_iface_t *viface)
/* We don't keep a reference to the target
* implementation, since that would give us a reference
* cycle. */
+ /* We do however keep a reference to the target
+ * schema, which we need to decrement before freeing
+ * the link */
+ avro_schema_decref(iface->schema);
avro_freet(avro_generic_link_value_iface_t, iface);
}
}
View
1 lang/c/tests/CMakeLists.txt
@@ -53,6 +53,7 @@ add_avro_test(test_avro_984)
add_avro_test(test_avro_1034)
add_avro_test(test_avro_1084)
add_avro_test(test_avro_1087)
+add_avro_test(test_avro_1165)
add_avro_test(test_avro_data)
add_avro_test(test_refcount)
add_avro_test(test_cpp test_cpp.cpp)
View
82 lang/c/tests/test_avro_1165.c
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <avro.h>
+
+/* To validate AVRO-1165, run this test program through valgrind
+ * before and after applying the AVRO-1165.patch. Before the patch
+ * valgrind will show memory leaks, and after the patch it will not.
+ * The specific valgrind commandline to use from the
+ * avro-trunk/lang/c/tests directory is:
+ * valgrind -v --track-origins=yes --leak-check=full
+ * --show-reachable = yes ../build/tests/test_avro_1165
+ */
+
+int main(int argc, char **argv)
+{
+ const char *json =
+ "{"
+ " \"name\": \"repeated_subrecord_array\","
+ " \"type\": \"record\","
+ " \"fields\": ["
+ " { \"name\": \"subrecord_one\","
+ " \"type\": {"
+ " \"name\": \"SubrecordType\","
+ " \"type\": \"record\","
+ " \"fields\": ["
+ " { \"name\": \"x\", \"type\": \"int\" },"
+ " { \"name\": \"y\", \"type\": \"int\" }"
+ " ]"
+ " }"
+ " },"
+ " { \"name\": \"subrecord_two\", \"type\": \"SubrecordType\" },"
+ " { \"name\": \"subrecord_array\", \"type\": {"
+ " \"type\":\"array\","
+ " \"items\": \"SubrecordType\""
+ " }"
+ " }"
+ " ]"
+ "}";
+
+ int rval;
+ avro_schema_t schema = NULL;
+ avro_schema_error_t error;
+ avro_value_iface_t *p_reader_class;
+
+ (void) argc;
+ (void) argv;
+
+ rval = avro_schema_from_json(json, strlen(json), &schema, &error);
+ if ( rval )
+ {
+ printf("Failed to read schema from JSON.\n");
+ return 1;
+ }
+ else
+ {
+ printf("Successfully read schema from JSON.\n");
+ }
+
+ p_reader_class = avro_generic_class_from_schema(schema);
+
+ avro_value_iface_decref(p_reader_class);
+
+ avro_schema_decref(schema);
+ return 0;
+}

0 comments on commit a258965

Please sign in to comment.