Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmap on Windows fails #22

Closed
ncw opened this issue Jan 23, 2019 · 0 comments
Closed

Unmap on Windows fails #22

ncw opened this issue Jan 23, 2019 · 0 comments

Comments

@ncw
Copy link
Contributor

ncw commented Jan 23, 2019

package main

import (
	"log"

	mmap "github.com/edsrzf/mmap-go"
)

func main() {
	const size = 1024 * 1024
	mem, err := mmap.MapRegion(nil, size, mmap.RDWR, mmap.ANON, 0)
	if err != nil {
		log.Fatalf("mmap: failed to allocate memory for buffer: %v", err)
	}
	log.Printf("memory %p", mem)
	err = mem.Unmap()
	if err != nil {
		log.Fatalf("mmap: failed to unmap memory for buffer: %v", err)
	}

}

This program works fine on linux, but under windows it prints this

X:\>go run mmap.go
2019/01/23 21:41:00 memory 0x31ba0000
2019/01/23 21:41:00 mmap: failed to unmap memory for buffer: FlushFileBuffers: The handle is invalid.
exit status 1

This is because FlushFileBuffers is being called on an anonymous handle.

This fixes it

--- a/vendor/github.com/edsrzf/mmap-go/mmap_windows.go
+++ b/vendor/github.com/edsrzf/mmap-go/mmap_windows.go
@@ -101,7 +101,7 @@ func (m MMap) flush() error {
 		return errors.New("unknown base address")
 	}
 
-	if handle.writable {
+	if handle.writable && handle.file != windows.Handle(^uintptr(0)) {
 		if err := windows.FlushFileBuffers(handle.file); err != nil {
 			return os.NewSyscallError("FlushFileBuffers", err)
 		}

Which I'll supply as a pull request in a moment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant