schema compile BUGFIX memory leak

Fixes #872
diff --git a/src/tree_schema_compile.c b/src/tree_schema_compile.c
index 3ba39ba..6155ee4 100644
--- a/src/tree_schema_compile.c
+++ b/src/tree_schema_compile.c
@@ -3670,8 +3670,10 @@
     /* the dflt member is just filled to avoid getting the default value from the type */
     leaf->dflt = (void*)leaf_p->dflt;
     ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
-    leaf->dflt = NULL;
-    LY_CHECK_RET(ret);
+    if (ret) {
+        leaf->dflt = NULL;
+        return ret;
+    }
 
     if (leaf_p->dflt) {
         struct ly_err_item *err = NULL;
diff --git a/tests/src/test_tree_schema_compile.c b/tests/src/test_tree_schema_compile.c
index 6ebe998..90fb86f 100644
--- a/tests/src/test_tree_schema_compile.c
+++ b/tests/src/test_tree_schema_compile.c
@@ -2298,7 +2298,7 @@
     assert_non_null(type);
     assert_int_equal(3, type->refcount); /* 2x type reference, 1x default value's reference (typedf's default does not reference own type)*/
     assert_int_equal(LY_TYPE_STRING, type->basetype);
-    leaf = (struct lysc_node_leaf*)mod->compiled->data;
+    assert_non_null(leaf = (struct lysc_node_leaf*)mod->compiled->data);
     assert_string_equal("hello", leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, NULL, NULL, &dynamic));
     assert_int_equal(0, dynamic);
     assert_string_equal("xxx", leaf->units);