  		obj->myalloc_ = 1;
  	}
  	if (!obj->buf_) {
-@@ -361,9 +361,12 @@ jas_stream_t *jas_stream_tmpfile()
- 	}
- 	obj->fd = -1;
- 	obj->flags = 0;
--	obj->pathname[0] = '\0';
+@@ -364,8 +364,11 @@ jas_stream_t *jas_stream_tmpfile()
+ 	obj->pathname[0] = '\0';
  	stream->obj_ = obj;
  
 +#ifdef _MSC_VER
-+	// http://www.christophlassner.de/collection-of-msvc-gcc-compatibility-tricks.html
-+	obj->pathname[0] = '\0';
-+
  	/* Choose a file name. */
- 	tmpnam(obj->pathname);
- 
-@@ -373,16 +376,22 @@ jas_stream_t *jas_stream_tmpfile()
+-	tmpnam(obj->pathname);
++	char *temppathname = tempnam(NULL, "jas");
++	strcpy(obj->pathname, temppathname);
++	free(temppathname);
+ 
+ 	/* Open the underlying file. */
+ 	if ((obj->fd = open(obj->pathname, O_CREAT | O_EXCL | O_RDWR | O_TRUNC | O_BINARY,
+@@ -373,16 +376,30 @@ jas_stream_t *jas_stream_tmpfile()
  		jas_stream_destroy(stream);
  		return 0;
  	}
 +#else
 +	/* Choose a file name. */
-+	snprintf(obj->pathname, L_tmpnam, "%stmp.XXXXXXXXXX", P_tmpdir);
++	snprintf(obj->pathname, L_tmpnam, "%s/tmp.XXXXXXXXXX", P_tmpdir);
 +
 +	/* Open the underlying file. */
 +	if ((obj->fd = mkstemp(obj->pathname)) < 0) {

-	since the file is open. */
 	if (unlink(obj->pathname)) {
-		/* We will try unlinking the file again after it is closed. */
-		obj->flags |= JAS_STREAM_FILEOBJ_DELONCLOSE;
+		jas_stream_destroy(stream);
+		return 0;
 	}
 
 	/* Use full buffering. */
@@ -553,7 +562,7 @@ int jas_stream_printf(jas_stream_t *stream, const char *fmt, ...)
 	int ret;
 
 	va_start(ap, fmt);

 	}
 
diff --git a/src/libjasper/base/jas_stream.c b/src/libjasper/base/jas_stream.c
index 9a88556..62d2d96 100644
--- a/src/libjasper/base/jas_stream.c
+++ b/src/libjasper/base/jas_stream.c
@@ -212,7 +212,7 @@ jas_stream_t *jas_stream_memopen(char *buf, int bufsize)
