diff options
Diffstat (limited to 'otherlibs/win32unix/close.c')
-rw-r--r-- | otherlibs/win32unix/close.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/otherlibs/win32unix/close.c b/otherlibs/win32unix/close.c index 48cd60e7a..21254ef1e 100644 --- a/otherlibs/win32unix/close.c +++ b/otherlibs/win32unix/close.c @@ -15,6 +15,9 @@ #include <mlvalues.h> #include "unixsupport.h" +#include <io.h> + +extern int _close(int); CAMLprim value unix_close(value fd) { @@ -24,9 +27,17 @@ CAMLprim value unix_close(value fd) uerror("close", Nothing); } } else { - if (! CloseHandle(Handle_val(fd))) { - win32_maperr(GetLastError()); - uerror("close", Nothing); + /* If we have an fd then closing it also closes + * the underlying handle. Also, closing only + * the handle and not the fd leads to fd leaks. */ + if (CRT_fd_val(fd) != NO_CRT_FD) { + if (_close(CRT_fd_val(fd)) != 0) + uerror("close", Nothing); + } else { + if (! CloseHandle(Handle_val(fd))) { + win32_maperr(GetLastError()); + uerror("close", Nothing); + } } } return Val_unit; |