      if (cur == NULL)
  	return (NULL);
      if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
-@@ -8205,8 +8218,8 @@
+@@ -8207,8 +8220,8 @@
          cur = ctxt->context->node;
          if (cur == NULL)
              return (NULL);

     if (cur == NULL) return(NULL) ; /* ERROR */
     if (cur->next != NULL) return(cur->next) ;
     do {
@@ -8160,8 +8168,13 @@
 xmlXPathNextPreceding(xmlXPathParserContextPtr ctxt, xmlNodePtr cur)
 {
     if ((ctxt == NULL) || (ctxt->context == NULL)) return(NULL);

         ctxt->ancestor = cur->parent;
     }
     if ((cur->prev != NULL) && (cur->prev->type == XML_DTD_NODE))
@@ -11737,11 +11750,16 @@
 
 	    if ((ctxt->error != XPATH_EXPRESSION_OK) || (res == -1)) {
 	        xmlXPathObjectPtr tmp;
-		/* pop the result */
+		/* pop the result if any */
 		tmp = valuePop(ctxt);
-		xmlXPathReleaseObject(xpctxt, tmp);
-		/* then pop off contextObj, which will be freed later */
-		valuePop(ctxt);
+		if (tmp != contextObj) {
+			/*
+			* Free up the result
+			* then pop off contextObj, which will be freed later
+			*/
+			xmlXPathReleaseObject(xpctxt, tmp);
+			valuePop(ctxt);
+		}
 		goto evaluation_error;
 	    }
 
