You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by dc...@apache.org on 2012/09/18 01:18:34 UTC

svn commit: r1386905 - in /avro/trunk: CHANGES.txt lang/c/src/generic.c lang/c/tests/CMakeLists.txt lang/c/tests/test_avro_1165.c

Author: dcreager
Date: Mon Sep 17 23:18:34 2012
New Revision: 1386905

URL: http://svn.apache.org/viewvc?rev=1386905&view=rev
Log:
AVRO-1165. C: Fix memory leak in AVRO_LINK generic value implementations

Contributed by Vivek Nadkarni.

Added:
    avro/trunk/lang/c/tests/test_avro_1165.c
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/c/src/generic.c
    avro/trunk/lang/c/tests/CMakeLists.txt

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1386905&r1=1386904&r2=1386905&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Mon Sep 17 23:18:34 2012
@@ -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

Modified: avro/trunk/lang/c/src/generic.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/src/generic.c?rev=1386905&r1=1386904&r2=1386905&view=diff
==============================================================================
--- avro/trunk/lang/c/src/generic.c (original)
+++ avro/trunk/lang/c/src/generic.c Mon Sep 17 23:18:34 2012
@@ -184,6 +184,10 @@ avro_generic_link_decref_iface(avro_valu
 		/* 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);
 	}
 }

Modified: avro/trunk/lang/c/tests/CMakeLists.txt
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/CMakeLists.txt?rev=1386905&r1=1386904&r2=1386905&view=diff
==============================================================================
--- avro/trunk/lang/c/tests/CMakeLists.txt (original)
+++ avro/trunk/lang/c/tests/CMakeLists.txt Mon Sep 17 23:18:34 2012
@@ -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)

Added: avro/trunk/lang/c/tests/test_avro_1165.c
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c/tests/test_avro_1165.c?rev=1386905&view=auto
==============================================================================
--- avro/trunk/lang/c/tests/test_avro_1165.c (added)
+++ avro/trunk/lang/c/tests/test_avro_1165.c Mon Sep 17 23:18:34 2012
@@ -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;
+}