Index: /trunk/mmap.c
===================================================================
--- /trunk/mmap.c	(revision 336)
+++ /trunk/mmap.c	(revision 337)
@@ -176,5 +176,5 @@
 		if (strncmp(shm->name, "/dev", 4) == 0) {
 			perror(shm->name);
-			errstr = "Cannot open file set by xcache.mmap_path";
+			errstr = "Cannot open file set by xcache.mmap_path, check the xcache.size/var_size against system limitation";
 			goto err;
 		}
@@ -183,5 +183,5 @@
 		if (fd == -1) {
 			perror(shm->name);
-			errstr = "Cannot open or create file set by xcache.mmap_path";
+			errstr = "Cannot open or create file set by xcache.mmap_path, check the path permission or check xcache.size/var_size against system limitation";
 			goto err;
 		}
Index: /trunk/xcache.c
===================================================================
--- /trunk/xcache.c	(revision 336)
+++ /trunk/xcache.c	(revision 337)
@@ -1541,11 +1541,18 @@
 		xc_php_caches = NULL;
 	}
+	xc_php_hcache.size = 0;
+
 	if (xc_var_caches) {
 		shm = xc_cache_destroy(xc_var_caches, &xc_var_hcache);
 		xc_var_caches = NULL;
 	}
+	xc_var_hcache.size = 0;
+	fprintf(stderr, "set 0\n");
+
 	if (shm) {
 		xc_shm_destroy(shm);
 	}
+
+	xc_initized = 0;
 }
 /* }}} */
@@ -1553,9 +1560,16 @@
 {
 	xc_shm_t *shm;
+	xc_shmsize_t shmsize = ALIGN(xc_php_size) + ALIGN(xc_var_size);
 
 	xc_php_caches = xc_var_caches = NULL;
+	shm = NULL;
+
+	if (shmsize < (size_t) xc_php_size || shmsize < (size_t) xc_var_size) {
+		zend_error(E_ERROR, "XCache: neither xcache.size nor xcache.var_size can be negative");
+		goto err;
+	}
 
 	if (xc_php_size || xc_var_size) {
-		CHECK(shm = xc_shm_init(xc_shm_scheme, ALIGN(xc_php_size) + ALIGN(xc_var_size), xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm");
+		CHECK(shm = xc_shm_init(xc_shm_scheme, shmsize, xc_readonly_protection, xc_mmap_path, NULL), "Cannot create shm");
 		if (!shm->handlers->can_readonly(shm)) {
 			xc_readonly_protection = 0;
@@ -1573,10 +1587,10 @@
 		}
 	}
-	return 1;
+	return SUCCESS;
 
 err:
+	xc_destroy();
 	if (xc_php_caches || xc_var_caches) {
-		xc_destroy();
-		/* shm destroied */
+		/* shm destroied in xc_destroy() */
 	}
 	else if (shm) {
@@ -2678,5 +2692,5 @@
 
 	if ((xc_php_size || xc_var_size) && xc_mmap_path && xc_mmap_path[0]) {
-		if (!xc_init(module_number TSRMLS_CC)) {
+		if (xc_init(module_number TSRMLS_CC) != SUCCESS) {
 			zend_error(E_ERROR, "XCache: Cannot init");
 			goto err_init;
@@ -2700,5 +2714,4 @@
 	if (xc_initized) {
 		xc_destroy();
-		xc_initized = 0;
 	}
 	if (xc_mmap_path) {
