Index: /trunk/xcache.c
===================================================================
--- /trunk/xcache.c	(revision 313)
+++ /trunk/xcache.c	(revision 314)
@@ -942,5 +942,5 @@
 	old_classinfo_cnt = zend_hash_num_elements(CG(class_table));
 	old_funcinfo_cnt  = zend_hash_num_elements(CG(function_table));
-	old_constinfo_cnt  = zend_hash_num_elements(EG(zend_constants));
+	old_constinfo_cnt = zend_hash_num_elements(EG(zend_constants));
 
 	zend_try {
@@ -1140,5 +1140,5 @@
 	zend_bool gaveup = 0;
 	zend_bool catched = 0;
-	zend_bool cached_php;
+	zend_bool newlycompiled;
 	char *filename;
 	char opened_path_buffer[MAXPATHLEN];
@@ -1229,12 +1229,21 @@
 	/* {{{ compile */
 	if (stored_php) {
-		cached_php = 1;
+		newlycompiled = 0;
 		xce.data.php = stored_php;
 	}
 	else {
-		cached_php = 0;
+		newlycompiled = 1;
 
 		/* make compile inside sandbox */
 		xc_sandbox_init(&sandbox, filename TSRMLS_CC);
+
+#ifdef HAVE_XCACHE_CONSTANT
+		php.constinfos  = NULL;
+#endif
+		php.funcinfos   = NULL;
+		php.classinfos  = NULL;
+#ifdef ZEND_ENGINE_2_1
+		php.autoglobals = NULL;
+#endif
 		zend_try {
 			op_array = xc_compile_php(&php, h, type TSRMLS_CC);
@@ -1242,12 +1251,9 @@
 			catched = 1;
 		} zend_end_try();
-		xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
-
+
+		xce.data.php = &php;
 		if (catched) {
-			cache->compiling = 0;
-			zend_bailout();
-		}
-
-		xce.data.php = &php;
+			goto err_aftersandbox;
+		}
 	}
 	/* }}} */
@@ -1273,5 +1279,5 @@
 	ENTER_LOCK_EX(cache) { /* {{{ php_store/entry_store */
 		/* php_store */
-		if (!cached_php) {
+		if (newlycompiled) {
 			stored_php = xc_php_store_dmz(&php TSRMLS_CC);
 			/* error */
@@ -1297,6 +1303,9 @@
 	cache->compiling = 0;
 	if (catched) {
-		zend_bailout();
-	}
+		goto err_aftersandbox;
+	}
+
+	xc_free_php(&php TSRMLS_CC);
+	xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
 
 	if (stored_xce) {
@@ -1311,4 +1320,14 @@
 		}
 		return xc_compile_restore(stored_xce, h TSRMLS_CC);
+	}
+	return op_array;
+
+err_aftersandbox:
+	xc_free_php(&php TSRMLS_CC);
+	xc_sandbox_free(&sandbox, 0 TSRMLS_CC);
+
+	if (catched) {
+		cache->compiling = 0;
+		zend_bailout();
 	}
 	return op_array;
