messages FEATURE implement free() functions from messages objects
diff --git a/src/messages.c b/src/messages.c
new file mode 100644
index 0000000..cdf4dd1
--- /dev/null
+++ b/src/messages.c
@@ -0,0 +1,63 @@
+/**
+ * \file messages.c
+ * \author Radek Krejci <rkrejci@cesnet.cz>
+ * \brief libnetconf2 - NETCONF messages functions
+ *
+ * Copyright (c) 2015 CESNET, z.s.p.o.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of the Company nor the names of its contributors
+ *    may be used to endorse or promote products derived from this
+ *    software without specific prior written permission.
+ *
+ */
+
+#include <stdlib.h>
+
+#include <libyang/libyang.h>
+
+#include "libnetconf.h"
+#include "messages_p.h"
+
+API void
+nc_rpc_free(struct nc_rpc *rpc)
+{
+    if (!rpc) {
+        return;
+    }
+
+    lyxml_free_elem(rpc->ctx, rpc->root);
+    lyd_free(rpc->tree);
+    free(rpc);
+}
+
+API void
+nc_reply_free(struct nc_reply *reply)
+{
+    if (!reply) {
+        return;
+    }
+
+    lyxml_free_elem(reply->ctx, reply->root);
+    lyd_free(reply->tree);
+    free(reply);
+}
+API void
+nc_notif_free(struct nc_notif *notif)
+{
+    if (!notif) {
+        return;
+    }
+
+    lyxml_free_elem(notif->ctx, notif->root);
+    lyd_free(notif->tree);
+    free(notif);
+}
diff --git a/src/messages.h b/src/messages.h
index 02d7294..7d505ef 100644
--- a/src/messages.h
+++ b/src/messages.h
@@ -38,4 +38,22 @@
  */
 struct nc_notif;
 
+/**
+ * @brief Free the NETCONF RPC object.
+ * @param[in] rpc Object to free.
+ */
+void nc_rpc_free(struct nc_rpc *rpc);
+
+/**
+ * @brief Free the NETCONF RPC reply object.
+ * @param[in] rpc Object to free.
+ */
+void nc_reply_free(struct nc_reply *reply);
+
+/**
+ * @brief Free the NETCONF Notification object.
+ * @param[in] rpc Object to free.
+ */
+void nc_notif_free(struct nc_notif *notif);
+
 #endif /* NC_MESSAGES_H_ */
diff --git a/src/messages_p.h b/src/messages_p.h
index 8452086..20bbfc7 100644
--- a/src/messages_p.h
+++ b/src/messages_p.h
@@ -28,16 +28,19 @@
 #include "messages.h"
 
 struct nc_rpc {
+    struct ly_ctx *ctx;
     struct lyxml_elem *root;
     struct lyd_node *tree;  /**< libyang data tree of the message */
 };
 
 struct nc_reply {
+    struct ly_ctx *ctx;
     struct lyxml_elem *root;
     struct lyd_node *tree;  /**< libyang data tree of the message */
 };
 
 struct nc_notif {
+    struct ly_ctx *ctx;
     struct lyxml_elem *root;
     struct lyd_node *tree;  /**< libyang data tree of the message */
 };
diff --git a/src/session.c b/src/session.c
index f54e80e..a1c28d6 100644
--- a/src/session.c
+++ b/src/session.c
@@ -108,7 +108,8 @@
 
     switch(msgtype) {
     case NC_MSG_RPC:
-        *rpc = calloc(1, sizeof **rpc);
+        *rpc = malloc(sizeof **rpc);
+        (*rpc)->ctx = session->ctx;
         (*rpc)->tree = lyd_parse_xml(session->ctx, xml, 0);
         (*rpc)->root = xml;
         break;
@@ -194,7 +195,8 @@
             }
 
             /* create notification object */
-            notif = calloc(1, sizeof *notif);
+            notif = malloc(sizeof *notif);
+            notif->ctx = session->ctx;
             notif->tree = lyd_parse_xml(session->ctx, xml, 0);
             notif->root = xml;
 
@@ -212,7 +214,8 @@
 
         switch(msgtype) {
         case NC_MSG_REPLY:
-            *reply = calloc(1, sizeof **reply);
+            *reply = malloc(sizeof **reply);
+            (*reply)->ctx = session->ctx;
             (*reply)->tree = lyd_parse_xml(session->ctx, xml, 0);
             (*reply)->root = xml;
             break;
@@ -292,7 +295,8 @@
         msgtype = nc_read_msg(session, timeout, &xml);
         if (msgtype == NC_MSG_REPLY) {
             /* create reply object */
-            reply = calloc(1, sizeof *reply);
+            reply = malloc(sizeof *reply);
+            reply->ctx = session->ctx;
             reply->tree = lyd_parse_xml(session->ctx, xml, 0);
             reply->root = xml;
 
@@ -310,7 +314,8 @@
 
         switch(msgtype) {
         case NC_MSG_NOTIF:
-            *notif = calloc(1, sizeof **notif);
+            *notif = malloc(sizeof **notif);
+            (*notif)->ctx = session->ctx;
             (*notif)->tree = lyd_parse_xml(session->ctx, xml, 0);
             (*notif)->root = xml;
             break;