path FEATURE duplicate value types in predicates

To avoid problems with the order of freeing
the type itself and then the predicate.
diff --git a/src/path.c b/src/path.c
index 0ec1d02..f590fbc 100644
--- a/src/path.c
+++ b/src/path.c
@@ -509,6 +509,9 @@
                     LYD_HINT_DATA, key, NULL, LY_VLOG_LYSC, key));
             ++(*tok_idx);
 
+            /* "allocate" the type to avoid problems when freeing the value after the type was freed */
+            ++((struct lysc_type *)p->value.realtype)->refcount;
+
             /* ']' */
             assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
             ++(*tok_idx);
@@ -553,6 +556,9 @@
                 LYD_HINT_DATA, ctx_node, NULL, LY_VLOG_LYSC, ctx_node));
         ++(*tok_idx);
 
+        /* "allocate" the type to avoid problems when freeing the value after the type was freed */
+        ++((struct lysc_type *)p->value.realtype)->refcount;
+
         /* ']' */
         assert(expr->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
         ++(*tok_idx);
@@ -973,6 +979,7 @@
                     /* key-predicate or leaf-list-predicate */
                     (*dup)[u].predicates[v].key = pred->key;
                     pred->value.realtype->plugin->duplicate(ctx, &pred->value, &(*dup)[u].predicates[v].value);
+                    ++((struct lysc_type *)pred->value.realtype)->refcount;
                     break;
                 case LY_PATH_PREDTYPE_NONE:
                     break;
@@ -1001,7 +1008,10 @@
             break;
         case LY_PATH_PREDTYPE_LIST:
         case LY_PATH_PREDTYPE_LEAFLIST:
-            predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value);
+            if (predicates[u].value.realtype) {
+                predicates[u].value.realtype->plugin->free(ctx, &predicates[u].value);
+                lysc_type_free((struct ly_ctx *)ctx, (struct lysc_type *)predicates[u].value.realtype);
+            }
             break;
         }
     }
diff --git a/src/path.h b/src/path.h
index 3f8cd19..fa48908 100644
--- a/src/path.h
+++ b/src/path.h
@@ -48,7 +48,7 @@
             uint64_t position;    /**< position value for the position-predicate */
             struct {
                 const struct lysc_node *key;    /**< key node of the predicate, NULL in case of a leaf-list predicate */
-                struct lyd_value value; /**< value representation according to the key's type */
+                struct lyd_value value; /**< value representation according to the key's type, its realtype is allocated */
             };
         };
     } *predicates;            /**< [Sized array](@ref sizedarrays) of the path segment's predicates */
diff --git a/src/tree_data.c b/src/tree_data.c
index cd1f4f0..9a40133 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -1237,6 +1237,7 @@
             ret = lyd_value_store(ctx, &pred->value, ((struct lysc_node_leaflist *)schema)->type, value, strlen(value),
                     NULL, LY_PREF_JSON, NULL, LYD_HINT_DATA, schema, NULL, LY_VLOG_LYSC, schema);
             LY_CHECK_GOTO(ret, cleanup);
+            ++((struct lysc_type *)pred->value.realtype)->refcount;
         } /* else we have opaq flag and the value is not valid, leavne no predicate and then create an opaque node */
     }