  	}
  
 diff --git a/src/libjasper/base/jas_stream.c b/src/libjasper/base/jas_stream.c
-index 9a88556..453adcf 100644
+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)

-	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);

 		obj->myalloc_ = 1;
 	}
 	if (!obj->buf_) {
@@ -364,8 +364,11 @@ jas_stream_t *jas_stream_tmpfile()
 	obj->pathname[0] = '\0';
 	stream->obj_ = obj;
 
+#ifdef _MSC_VER
 	/* Choose a file name. */
-	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, "%s/tmp.XXXXXXXXXX", P_tmpdir);
+
+	/* Open the underlying file. */
+	if ((obj->fd = mkstemp(obj->pathname)) < 0) {
